使用COM 1、首先是准备工作: 在Windows下通过PHP访问MSSQL SERVER 2000,可以有两种方式, (1)利用COM (2)一种是使用MSSQL_系列函数 要使用这两种都需要在php.ini进行设定: (1)允许 DCOM,需要将php.ini中的 ;com.allow_dcom=TRUE前的分号";"去掉。 (2)使用
使用COM
1、首先是准备工作:
在Windows下通过PHP访问MSSQL SERVER 2000,可以有两种方式,
(1)利用COM
(2)一种是使用MSSQL_系列函数
要使用这两种都需要在php.ini进行设定:
(1)允许 DCOM,需要将php.ini中的 ;com.allow_dcom=TRUE前的分号";"去掉。
(2)使用MSSQL扩展,需要php.ini中的 ;extension=php_mssql.dll前的分号";"去掉。
(3)确认extension_dir为正确路径,以本机为例:extension_dir = "c:/AppServ/php5/ext"。
(4)如果仍然机器报错说找不到c:/AppServ/php5/ext/php_mssql.dll但明明存在这个文件。
解决方法:将php_mssql.dll,ntwdblib.dll拷贝到系统目录/system32下重启测试。。
(注:上面两个dll文件不在相同目录下,我的为c:/AppServ/php5/ext/php_mssql.dll;c:/AppServ/php5/ntwdblib.dll)
另外设置好了后记得重启服务器哦。
我的PHP环境是用AppServ搭建的,我这样做了后,访问MSSQL SERVER一切正常。若你仍然有问题,请查阅PHP手册php.ini的设置。
2、利用COM访问MSSQL SERVER的简单方法
$conn = new COM("ADODB.Connection") or die("Cannot start ADO");
//首先声明一个连接对象
$connstr = "Provider=SQLOLEDB;
Persist Security Info=False;
User ID=youruid;
Password=yourpwd;
Initial Catalog=yourdatabase;
Data Source=127.0.0.1";
//设置连接字符串(很奇怪Data Soure为localhost或服务器名不行总是报错而为(local)或127.0.0.1却行,希望哪个高手帮我说明一下)
$conn->Open($connstr); //建立数据库连接
$sqlstr = "select * from test"; //设置查询字符串
$rs = $conn->Execute($sqlstr); //执行查询获得结果
或者
//$rs = new com("adodb.recordset"); //声明一个数据集对象
//$rs->open($sqlstr,$conn);//,3,3); //获得数据集rcordset内容
$num_cols = $rs->Fields->Count(); //得到数据集列数
while (!$rs->EOF) //输出结果
{
echo $rs->Fields['name']->Value."
"; // 'name'为字段名,需要明确制定
$rs->MoveNext();
或者
//for ($i=0; $i //{
//echo $fld[$i]->value . "/t";
//}
//echo "/n";
//$rs->MoveNext();
}
$rs->Close();
$conn->Close();
$rs = null;
$conn = null;
3、使用MSSQL_函数访问MSSQL SERVER的简单方法
$hostname = "yourhoustname";
$username = "yourusername";
$password = "yourpassword";
$conn = mssql_connect($hostname,$username,$password) or die("DATABASE FAILED TO RESPOND.");
//建立连接
$dbName = "yourdatabase";
$ret=mssql_select_db($dbName); //选择数据库
$sqlstr = "select * from test"; //设置查询字符串
$result=mssql_query($query); //执行查询获得结果
while($line = mssql_fetch_row($result)) //输出结果
{
echo "$line[0],$line[1]";
//mssql_fetch_row每次返回一个enumerated(列举) array,直到没有数据而返回false
}
或者
//while($row = mssql_fetch_array($result))
//{
//echo $row["name"];
或者
//echo $row[0],$row[1];
//mssql_fetch_array每次返回一个数字索引数组或一个相关数组,直到没有数据而返回false
//}
题外:
关于通过PHP访问MSSQL SERVER的小结就是这些了。希望能对你有所帮助,第一次写又是初学,请你指出,共同进步。
求助:
1、我在设置连接字符串的时候,为什么Data Soure为localhost或服务器名不行总是报错而为(local)或127.0.0.1却行,希望哪个高手帮我说明一下
2、我在设置查询字符串的时候,如何对"LIKE '%-'"(单引号内为一个百分号和一个短横线)进行转义
使用pdo
1、首先仍然是准备工作:
(1)使用PDO扩展,需要将php.ini中的 ;extension=php_pdo.dll前的分号";"去掉。
(2)访问MSSQL,需要将php.ini中的 ;extension=php_pdo_mssql.dll前的";"去掉(没有则添加)。
补充:如果你需要访问MySQL,需要将php.ini中的 ;extension=php_pdo_mssql.dll前的";"去掉。
其他数据库请去掉相应扩展前的";"。
(3)确认extension_dir为正确路径,以本机为例:extension_dir = "c:/AppServ/php5/ext"。
(4)如果仍然机器报错说找不到c:/AppServ/php5/ext/php_mssql.dll但明明存在这个文件。
解决方法:将php_mssql.dll,ntwdblib.dll拷贝到系统目录/system32下重启测试。。
(注:上面两个dll文件不在相同目录下,我的为c:/AppServ/php5/ext/php_mssql.dll;c:/AppServ/php5/ntwdblib.dll)
另外:设置完毕后仍然记得要重启服务器哦
2、使用PDO访问MSSQL SERVER的简单方法
$username = yourusername;
$pwd = yourpassword;
try
{
//??host=127.0.0.1可用;(local)不可用;localhost不可用;MAXY不可用
$dbh = new PDO('mssql:host=127.0.0.1;dbname=yourdatabase',$username,$pwd);
//声明一个PDO对象并指定它的连接字符串
$sqlstri = "select * from UpLow";
//指定查询字符串
foreach ($dbh->query($sqlstri) as $row)
//执行查询获得结果,并输出
{
print_r($row);
或者
//echo $row['name'];
//指定字段输出
}
}
catch (PDOException $e)
{
print "Error!;".$e->getMessage()."
";
}
http://blog.csdn.net/fkedwgwy/archive/2008/12/09/3484757.aspx

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools