Stay hungry, Stay foolish

0%

[win]php连接mssql

php5.2.*

连接mssql 2005以下的版本在php.ini里把extension=php_mssql.dll前的分号去掉重启web服务器就行了。

如果mssql版本在2005及以上的话,在完成上述的基础上还要下载新版的ntwdblib.dll( 2000.80.194.0)放到apache的bin目录下。(我安装的apache2.2.8,bin下有个默认的ntwdblib.dll,版本是2000.2.8.0,直接覆盖之)。

php5.3.*

php从5.3开始默认不支持sql server。庆幸的是微软反过来对php支持。所要要想连接sql server的话就要费一番周张:

解压后有针对各个版本的扩展,还有文档。(也可单独下载)

  • 查看自己的php版本

一般要了解的就是VC6、VC9、TS和NTS,打开phpinfo,Compiler选项是MSVC6的话就是是VC6,Thread Safety选项是enabled就是TS。我的是php5.3.5-vc6-ts。就把php_sqlsrv_53_ts_vc6.dll拷到php的extension目录下。

  • 在php.ini里开启对sqlsrv的支持
1
extension=php_sqlsrv_53_ts_vc6.dll
  • 重启web服务器,打开sqlsrv手册,并测试
1
2
3
4
5
6
7
8
9
10
11
12
$serverName = "server_ip,port";
$connectionInfo = array( “Database”=>"dbname","UID"=>"username","PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
echo "Connection established.n";
}
else
{
echo "Connection could not be established.n";
die( print_r( sqlsrv_errors(), true));
}

错误整理

  • This extension requires either the Microsoft SQL Server 2008 Native Client (SP1 or later) or the Microsoft SQL Server 2008 R2 Native Client ODBC Driver to communicate with SQL Server. Neither of those ODBC Drivers are currently install.

这是因为要连接的mssql的版本比较高,需要安装SQL Server Native Client

据说打赏我的人,代码没有BUG