1

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

https://learn.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 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.