Skip to content
Advertisement

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

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!

Advertisement

Answer

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 ManagementServices and ApplicationsSQL Server Configuration Manager), then go to SQL Server Network Configuration, Protocols for “InstanceName”. First, TCP/IP must be enabled. Then open Properties for TCPIP. 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.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement