PHP sqlsrv_connect 错误
Posted
技术标签:
【中文标题】PHP sqlsrv_connect 错误【英文标题】:PHP sqlsrv_connect Error 【发布时间】:2018-05-02 21:54:26 【问题描述】:我正在尝试使用 php 连接到 MS SQL Server,但失败了。我正在运行 PHP7.2,并且我已经安装了 Microsoft Drivers 5.2 for PHP for SQL Server 和 Microsoft ODBC Driver 17 for SQL Server。
这是我正在使用的代码...
$serverName = "severname\MSSQLSERVER";
$conn_array = array (
"UID" => "username",
"PWD" => "password",
"Database" => "databasename",
);
$conn = sqlsrv_connect($serverName, $conn_array);
if ($conn)
echo "connected";
else
die(print_r(sqlsrv_errors(), true));
这是我收到的错误消息...
> Array (
> [0] => Array (
> [0] => 08001
> [SQLSTATE] => 08001
> [1] => 87
> [code] => 87
> [2] => [Microsoft][ODBC Driver 17 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87].
> [message] => [Microsoft][ODBC Driver 17 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87].
> ) [1] => Array (
> [0] => HYT00 [SQLSTATE] => HYT00
> [1] => 0 [code] => 0
> [2] => [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired
> [message] => [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired
> ) [2] => Array (
> [0] => 08001
> [SQLSTATE] => 08001
> [1] => 87 [code] => 87
> [2] => [Microsoft][ODBC Driver 17 for SQL Server]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][ODBC Driver 17 for SQL
> Server]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.
> )
> )
更新: 删除实例名称后出现此错误... SQLSTATE[28000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]用户 'USERNAME 登录失败'。
【问题讨论】:
您的实例名称/又名架构名为MSSQLSERVER
?不清楚您可能混淆了什么或可能完全错误。
感谢您提供帮助。删除实例名称后,我收到此错误... SQLSTATE[28000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]用户 'USERNAME' 登录失败。
请更新您遇到的新错误问题。
【参考方案1】:
正如@ficuscr 所暗示的,您的问题很可能是实例名称。 MSSQLSERVER
是 default 实例的实例名称,您不使用 server\instance
格式引用它。您只需使用server
。您只需对命名实例使用server\instance
格式。
假设是这种情况,您的第一行应该是
$serverName = "severname";
【讨论】:
当我删除实例名称并仅使用服务器名称时,我收到以下错误... Array ([0] => Array ([0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ ODBC Driver 17 for SQL Server][SQL Server]用户 USERNAME' 登录失败。[消息] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]用户 'USERNAME' 登录失败。)[1] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]登录失败对于用户'USERNAME'。[消息] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]用户'USERNAME'登录失败。)) 这真的很好!如果您收到该错误,那么您至少已经成功地与 SQL Server 建立了网络连接,该连接比您之前的距离更远。现在您只需要修复您传递的用户名和密码。 “密码”?太棒了!我的行李箱上也有同样的组合! @ficuscr 经典。可能只是在 dvd 播放器中弹出它。以上是关于PHP sqlsrv_connect 错误的主要内容,如果未能解决你的问题,请参考以下文章
致命错误:使用 wamp php 5.5.12 调用未定义函数 sqlsrv_connect()
致命错误:未捕获的错误:调用 C:\xampp\htdocs\DBtest\sqlsrv.php:7 中的函数 sqlsrv_connect() 堆栈跟踪:#0 main SQLSERVER
未捕获的错误:调用未定义的函数 sqlsrv_connect()
PHP sqlsrv_connect 到 SQL Server:建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误