【问题标题】:can't establish connection to sql server (PHP) [duplicate]无法建立与 sql server(PHP)的连接 [重复]
【发布时间】:2015-10-09 15:17:33
【问题描述】:

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

我可以使用 SQL Server Management Studio 远程访问服务器。

如果我运行:

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

我明白了:

Server_Name\Instance_Name

(也许值得注意的是,我无法使用此 Server_Name 连接到 Management Studio 中的服务器 - 我必须使用公共 IP,不知道为什么,但我是菜鸟,所以这可能是正常的?)

我正在运行的 PHP 脚本(在我用来建立 Management 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. ) )

【问题讨论】:

  • 这里有语法错误$password = "same_pass_as_for_management_studio"'; 一方面。它抛弃了语法高亮。
  • @Fred-ii- 当我为了发布而更改变量名并且实际代码中不存在时发生这种情况,但谢谢
  • 呃...懒惰阅读-.-
  • 看在上帝的份上,让那个错误可读人.. 呃.. 有换行符和一切......顺便说一句 -> 是stackoverflow.com/questions/28659796/… 无论如何相关?
  • 不客气(:然后我将其标记为重复

标签: php sql-server


【解决方案1】:

SQL 服务器是否在默认端口上运行?

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-17
    • 2017-10-20
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    相关资源
    最近更新 更多