我尝试使用PHP建立与SQL服务器的连接
我可以使用SQL Server Management studio远程访问服务器。
然后如果我跑:
select @@servername + '\' + @@servicename
我明白了:
Server_Name\Instance_Name
(也许值得注意的是我无法使用此Server_Name连接到Management Studio中的服务器 - 我必须使用公共IP,不知道为什么,但我是noob所以也许那是正常的吗?)
我正在运行的PHP脚本(在我用来建立Management Studio Connection的同一台机器上)如下所示:
<?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. ) )
答案 0 :(得分:0)
SQL服务器是否在默认端口上运行?
$serverName = "serverName\sqlexpress, 1542"; //serverName\instanceName, portNumber (default is 1433)