2009-04-28 87 views
0

我想在Windows XP SP2上用PHP 5.2.9-2連接到SQL Server 2008(不是表達式)。我可以從同一臺機器上的SQL Server Management Studio連接到遠程SQL Server。用PDO連接到SQL Server 2008

我第一次嘗試是這樣的:

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

哪個給出了這樣的錯誤:

 
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)'

第二次嘗試(上experts-exchange找到)

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

這工作,但我可以」 t使用PDO::lastInsertId(),我希望能夠:

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

你有什麼建議嗎?任何幫助將不勝感激。

回答

1

我很確定pdo與mssql數據服務器類型需要dblib連接。你在機器上安裝了dblib嗎?確保你可以在系統的路徑中的某處找到ntwdblib.dll。我知道這不會與您收到的錯誤消息一起玩,但我不確定我是否相信錯誤消息。

0

That you cannot use SQL Server authentication because only Windows authentication is permitted.

檢查服務器是否running Mixed mode authentication.

同時檢查this SO question幫助你。

+0

感謝您的支持。另一個問題雖然沒有解決。它運行的是混合模式,因爲我可以通過使用Windows身份驗證或使用爲數據庫創建的SQL Server登錄名通過SQL Server Management Studio進行連接。 – 2009-04-28 13:38:55

1

我更新了ntwdblib.dll建議here,它現在可以工作。

不幸的是,我仍然不能使用PDO::lastInsertId(),因爲顯然這個驅動程序也不支持它,所以它不是真的值得的麻煩。但我可以使用等效的SELECT @@IDENTITY as Id