无法建立连接到sql server (PHP)


can't establish connection to sql server (PHP)

我正在尝试使用PHP建立到SQL服务器的连接

我可以使用SQL server Management studio远程访问服务器。

然后如果我运行:

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

:

Server_Name'Instance_Name

(也许值得注意的是,我不能使用这个Server_Name连接到管理工作室的服务器-我必须使用公共IP代替,不知道为什么,但我是新手,所以也许这是正常的?)

我正在运行的PHP脚本(在我用来建立管理工作室连接的同一台机器上)看起来像这样:

<?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. ) )

SQL server是否运行在默认端口上?

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