简体   繁体   English

使用PDO连接到SQL Server 2008

[英]Connect to SQL Server 2008 with PDO

I am trying to connect to SQL Server 2008 (not express) with PHP 5.2.9-2 on Windows XP sp2. 我正在尝试使用Windows XP sp2上的PHP 5.2.9-2连接到SQL Server 2008(未表达)。 I can connect to the remote SQL Server fine from SQL Server Management Studio from the same machine. 我可以从同一台计算机上的SQL Server Management Studio中连接到远程SQL Server。

My first attempt was this: 我的第一次尝试是:

$conn = new PDO("mssql:host={$host};dbname={$db}", $user, $pass);

Which gives this error: 出现此错误:

PHP Fatal error:  
Uncaught exception 'PDOException' with message 
'SQLSTATE[0100] Unable to connect: SQL Server is unavailable or does not exist.  
Access denied. (severity 9)'

Second attempt (found on experts-exchange ) 第二次尝试(在expert-exchange上找到)

$conn = new PDO("odbc:Driver={SQL Server Native Client 10.0};Server={$host};Database={$db};Uid={$user};Pwd={$pass}", 
                         $user, $pass);

This works, but I can't use PDO::lastInsertId() , which I would like to be able to: PDO::lastInsertId() ,但是我不能使用PDO::lastInsertId() ,我希望能够:

Driver does not support this function: driver does not support lastInsertId()

Do you have any suggestions? 你有什么建议吗? Any help would be greatly appreciated. 任何帮助将不胜感激。

i'm pretty sure that pdo with the mssql data server type requires dblib to connect. 我非常确定具有mssql数据服务器类型的pdo需要dblib进行连接。 do you have dblib installed on the machine? 您是否在计算机上安装了dblib? make sure you can find ntwdblib.dll somewhere in the path on your system. 确保您可以在系统路径中的某处找到ntwdblib.dll。 i know this doesn't jive with the error message you're getting, but i'm not sure i trust the error message. 我知道这与您收到的错误消息并不吻合,但是我不确定我是否信任错误消息。

I updated ntwdblib.dll as suggested here and it now works. 我按照这里的建议更新了ntwdblib.dll,现在可以使用了。

Unfortunately I still can't use PDO::lastInsertId() because apparently this driver does not support it either, so it wasn't really worth the hassle. 不幸的是,我仍然不能使用PDO::lastInsertId()因为显然该驱动程序也不支持它,因此它真的不值得麻烦。 I can however use the equivalent SELECT @@IDENTITY as Id . 但是,我可以将等效的SELECT @@IDENTITY as Id

That you cannot use SQL Server authentication because only Windows authentication is permitted. 您不能使用SQL Server身份验证,因为仅允许Windows身份验证。

Check if the server is running Mixed mode authentication. 检查服务器是否正在运行混合模式身份验证。

Also check if this SO question helps you. 还要检查此SO问题是否对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM