研究了很久,终于发现:最新的php 5.3.6中php_mssql.dll,php_pdo_mssql.dll都已经不见了。

只有一个php_pdo_odbc.dll。

so~最新最好的php连接mssql方法应该是这样:
$cnx = new PDO("odbc:Driver={SQL Server};Server=127.0.0.1;Database=test;",'sa','asd123');
var_dump($cnx);
$a = $cnx->query("SELECT * FROM [user]");
var_dump($a);
foreach ($a as $b) {
var_dump($b);
}
?>

关于PHP无法连接MSSQL数据库的问题今天配置了新服务器,配置是IIS+php,结果运行时发现php连接远程mssql数据库出错,出错代码如下:

Warning: mssql_connect(): Unable to connect to server:

想想以前都是没问题的,怎么回事呢?后来去网上搜索,发现一篇文章,才发现原来服务器是需要安装mssql才能用php连接mssql的,本来新服务器上我是不需要用到mssql的,但是现在没办法了,只能把它装上了,安装了mssql之后就没问题了。

我在想,如果是在linux上面的apache+php会怎样呢,不可能需要安装mssql吧,呵呵,晕了。


php配置:

在php.ini文件里设置如下,找到

;extension=php_mssql.dll 把前面的分号去掉

找到extension_dir = d:\extension\

你的php.ini里面可能不是d:\extension\

改成在php安装目录下面的extensions目录下面的php_mssql.dll,所在的路径,如果你没有把它移动到其他地方(假设你的php安装路径是d:\php)

就改成extension_dir=d:\php\extensions\

然后重新启动web服务器

这一点很容易做到,但是做完这样的设置后还是连不上,错误的信息如下:

MS SQL Server 数据库连接错误!请检查数据库主机变量设置是否正确!!!

而主机的变量设置我是一遍一遍的检查,那些设置是一点问题都没有的,翻遍网页,找到了下面的这点蛛丝马迹:


I am trying to connect to SQL Server 2000 from PHP
I bumped to following warning:
Warning: mssql_connect(): Unable to connect to server: SERVER\Portal
....... on line 5
on line 5 there is:
$db_connect = mssql_connect('SERVER\Portal', 'sa', 'my_passwd');
I did the following
1.enabled php_mssql.dll extension in PHP.ini
2.every dll is in proper place(System32 or PHP folder),including ntwdblib.dll
I search lots of profile throught web ,but no one give me proper answer to resolve it.
after a few hour ,I found the problem was caused by
ntwdblib.dll ,which version is 7.00.839 ,when I replaced old ntwdblib.dll with the new
ntwdblib.dll ,which version is 8.00.194 ,all problem are solved.
We had some, read A LOT, of problems with MSSQL under Windows 2003.
We had 2 the same windows, php, php-ini, everything machines but only one could connect.
Unable to connect was the error message.
Finnaly we checked the version of ntwdblib.dll and the one distributed with PHP was 7.00....
and the version of the one on the SQL Server install was 8.00.... so we copied this one in
the php and apache dir and it worked.

问题就这样被找到了,惹祸的是它 ntwdblib.dll

ntwdblib.dll的主要作用是提供sql server连接服务。