PHP sqlsrv_connect to SQL Server: A network-related or instance-specific error has occurred while establishing a connection to SQL Server

ⅰ亾dé卋堺 提交于 2020-06-17 03:33:32

问题


I want to connect PHP 7.1 to SQL Server. I add compatible drivers from:

https://docs.microsoft.com/en-us/sql/connect/php/microsoft-php-driver-for-sql-server

    try {
        $serverName = "tcp:localhost,1433";
        $connectionOptions = array("Database" => "fc","Uid" => "sa", "PWD" => 
"Aa123456");
        $conn = sqlsrv_connect($serverName, $connectionOptions);
        if ($conn == false){
            die (print_r(sqlsrv_errors ()));}
    } catch (Exception $e) {
        echo("Error!");
    }

to my php.ini and I checked that in phpinfo() Now I have ODBC 13.1 on my windows 10 and install SQL server 2016 correctly and can login with "sa" user via sql server management studio. but when I try this code:

I get this error: [Microsoft][ODBC Driver 13 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.

Please help to find the answer... thank you!


回答1:


I get this error when MS SQL Server instance is not set correctly.

Possible solution:

You should check your server network configuration. Go to SQL Server Configuration Manager (or Computer Management\Services and Applications\SQL Server Configuration Manager), then go to SQL Server Network Configuration, Protocols for "InstanceName". First, TCP/IP must be enabled. Then open Properties for TCP\IP. On "IP Addresses" tab, go to IPAll. For this script to work "TCP Dynamic ports" must be empty, and "TCP Port" must equals to 1433 (or some other port).

Note:

If "SQL Server Browser service" is running, you can connect without port number.



来源:https://stackoverflow.com/questions/49533539/php-sqlsrv-connect-to-sql-server-a-network-related-or-instance-specific-error-h

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!