Ehphan
Ehphan

Reputation: 537

mssql_connect(): Unable to connect to server

I have a Centos server with apache and PHP on it, and I have a SQL Server on another server, when I try to connect to MSSQL with tsql -H "sql server ip" -p1433 -U sa, it works and connects OK, but when I try to run following PHP code

error_reporting(E_ALL);
ini_set('display_errors', '1');
$connection = mssql_connect('serverIP', 'username', 'password');

I get the error :

Warning: mssql_connect(): Unable to connect to server: serverIP in /var/www/html/mssql.php on line 4

what should I do?

Upvotes: 2

Views: 35959

Answers (2)

Orabîg
Orabîg

Reputation: 11992

In my case, I noticed that I coulnd't connect when I was giving the DB server name, but when I gave the IP adress directly, it worked !

Maybe this information could be helpful to others...

Edit : after some research, I think this can be related to the freetds.conf file, which may contain server definitions. When using a name instead of an IP, I'm pretty sure that the driver is looking for a TDS definition of the same name...

Upvotes: 1

Mihai
Mihai

Reputation: 26784

You have to specify the servername and the instance like in this example,I think: https://www.php.net/manual/en/function.mssql-connect.php

EDIT: The following method: server\instance does'nt work on unix systems...

An instance is nothing more than specific port address different than 1433... so just discover the port on mssql server and then try to connect using:

ip:port

In unix the port is specified by : not by ,

Upvotes: 6

Related Questions