2015-10-09 198 views
0

我試圖使用PHP建立到SQL服務器無法建立連接到SQL Server(PHP)

連接我可以遠程使用SQL Server Management Studio中訪問該服務器。

然後,如果我運行:

select @@servername + '\' + @@servicename 

我得到:

Server_Name\Instance_Name 

(也許這是值得注意的是,我不能使用此服務器名稱來連接到Management Studio中的服務器 - 我有使用公共IP,不知道爲什麼,但我是noob,所以也許這是正常的?)

我正在運行的PHP腳本(在我用於建立管理Studio連接的同一臺機器上)外觀像這樣:

<?php 

$username = "same_user_as_for_management_studio"; 

$password = "same_pass_as_for_management_studio"; 

$serverName = "Server_Name\Instance_Name"; 

$db = "Example"; 


$connectionInfo = array("Database"=> $db, "UID"=>$username, "PWD"=>$password); 
$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)); 
} 
?> 

我試着用服務器名稱代替公有IP,但仍然沒有喜悅。我得到的錯誤是:

Array ([0] => Array ([0] => 08001 [SQLSTATE] => 08001 [1] => 87 [code] => 87 [2] => [Microsoft][SQL Server Native Client 11.0]SQL Server Network Interfaces: Connection string is not valid [87]. [message] => [Microsoft][SQL Server Native Client 11.0]SQL Server Network Interfaces: Connection string is not valid [87].) [1] => Array ([0] => HYT00 [SQLSTATE] => HYT00 [1] => 0 [code] => 0 [2] => [Microsoft][SQL Server Native Client 11.0]Login timeout expired [message] => [Microsoft][SQL Server Native Client 11.0]Login timeout expired) [2] => Array ([0] => 08001 [SQLSTATE] => 08001 [1] => 87 [code] => 87 [2] => [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. [message] => [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.)) 
+1

語法錯誤在這裏'$ password =「same_pass_as_for_management_studio」';'一件事。它拋出了語法突出顯示。 –

+0

@ Fred-ii-發生這種情況時,我爲了發佈而更改了變量名稱,並且沒有出現在實際代碼中,但是謝謝 –

+0

您的意思恰恰與「無法建立連接」有關嗎?你有什麼錯誤信息嗎? – Paolo

回答

0

SQL服務器是否在默認端口上運行?

$serverName = "serverName\sqlexpress, 1542"; //serverName\instanceName, portNumber (default is 1433)