2015-10-09 200 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)