Search code examples
phpsql-serversqlsrv

Can't connect to sql from php with ip


So i have this script for connection to sql server.

//SET CONNECTION TO MSQL
$serverName = "";
$uid = "user";
$pwd = "password";
$databaseName = "database";

//BUILD CONNECTION ARRAY
$connectionInfo = array( "UID"=>$uid,
                         "PWD"=>$pwd,
                         "Database"=>$databaseName);

/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);


//TEST CONNECTION
if( $conn ) {
echo "Connection established.<br />";

}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}

I have this script localy on the server and remote.
My mission is to be able to connect remotely.

So if i try $serverName = "SRV\SQLEXPRESS"; and runt this localy i gott message: Connection established.

If i change the server name for the local ip and test it locally i got this message:

Connection could not be established. Array ( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 2 [code] => 2 [2] => [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server

I have been talking to the network administration and they say that the port is open and the firewall accepts connections both locally and remote.

If i try to connect remotely i got the message that tha odbc driver cannot be found.

So what seems to be the problem, and what should i do to find it? What can i test?

EDIT

As suggested in the comments i tried:

$serverName = "tcp:192.168.0.242, 51484";

And it worked. I dont know why that port is beeing used? sql configuration manager says 1433 is used?

Remote connection to public ip still tells me that odbc driver is missing?!


Solution

  • As @preben said in the comments, this was solved by adding "tcp" to the serverName.

    $serverName = "tcp:192.168.0.242, 51484";