2012-12-05 35 views
3

我試圖建立一個連接到我公司的MSSQL服務器與Windows身份驗證,但它失敗,因爲它試圖使用我的計算機名稱作爲登錄而不是我的登錄ID。當與MS SQL Server管理登錄在windows身份驗證工作正常,但與此PHP代碼:PHP試圖用計算機名稱而不是登錄名登錄到MSSQL(Windows身份驗證)

<?php 
// Server in the this format: <computer>\<instance name> or 
// <server>,<port> when using a non default port number 
$serverName = "xxx"; 
$connectionInfo = array("Database"=>"xxx"); 

/* Connect using Windows Authentication. */ 
$conn = sqlsrv_connect($serverName, $connectionInfo); 


if($conn) { 
    echo "Connection established.<br />"; 
}else{ 
    echo "Connection could not be established.<br />"; 
    die(print_r(sqlsrv_errors(), true)); 
} 

// 
?> 

打印輸出我得到的是以下幾點:

[微軟] [SQL Server本機客戶端11.0] [SQL Server] 用戶'xxx \ T2002197 $'登錄失敗。 ))

T2002197是我的電腦名稱,而不是我的登錄ID,所以當然它失敗了。我該如何解決這個問題?我正在使用WAMP。 編輯出一些信息,替換爲'xxx'

回答

0

該手冊指出,默認情況下連接使用Windows身份驗證而不是SQL Server身份驗證。

要繞過此,您需要在您的$connectionInfo數據中提供uid和pwd選項。

查看說明書;

http://php.net/sqlsrv_connect

http://msdn.microsoft.com/en-us/library/ff628167.aspx

+0

是的,但我在SQL服務器上的帳戶只允許使用Windows身份驗證,而不是普通的SQL Server身份驗證。例如,在SQL Server Management Studio中,我只能使用Windows身份驗證登錄,如果嘗試手動輸入我的登錄名/密碼,則失敗。所以我需要使它以某種方式使用Windows身份驗證... – Suedish

7

啊哈問題解決了!我更改了我的WAMP服務(在Windows中打開service.msc)的設置,並確保服務記錄在正確的帳戶上。它現在有效。

+0

這很棒@suedish – Bukky