PHP sqlsrv_connect错误

时间:2018-05-02 21:54:27

标签: php sql sql-server

我尝试使用PHP连接到MS SQL Server并失败。我正在运行PHP7.2,我已经为SQL Server安装了Microsoft Drivers 5.2 for SQL Server,为SQL Server安装了Microsoft ODBC Driver 17。

以下是我使用的代码......

$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] [SQL Server的ODBC驱动程序17] [SQL Server]用户登录失败&# 39; USERNAME'

1 个答案:

答案 0 :(得分:2)

正如@ficuscr暗示的那样,很可能你的问题是实例名称。 MSSQLSERVER是您未使用server\instance格式引用的默认实例的实例名称。您只需使用server即可。您只需对命名实例使用server\instance格式。

假设是这种情况,你的第一行应该是

$serverName = "severname";