3

I'm running PHP 5.5 on CentOS 6 trying to connect to a MS SQL Server database via PDO. I have searched through about 20 different answers to the same error message and tried every one of them here on SO. When I attempt to connect from PDO I get the following error:

SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)

I am able to connect through FreeTDS on the command line with no issue at all:

TDSVER=7.0 tsql -H 192.168.5.57 -p 1433 -U testuser

I tried setting tds version = 7.0 in freetds.conf and still get the same error.

I've tried connecting to different MS SQL Servers and get the same thing every time. I've tried this same code on a Rackspace Cloud server with no issues. So something between PDO/PHP and FreeTDS doesn't seem to be talking correctly, but I am at a loss of how to track this down.

try {
  $hostname = '192.168.5.57:1433';
  $dbname = 'Test';
  $username = 'testuser';
  $password = 'thisismypassword';
  $db = new PDO("dblib:host=$hostname;dbname=$dbname","$username","$password");
} catch (PDOException $e) {
  echo 'Failed to get DB handle: ' . $e->getMessage();
}
chris85
  • 23,846
  • 7
  • 34
  • 51
Jeremy H.
  • 523
  • 1
  • 5
  • 18
  • Oh my lord. Good luck interfacing MSSQL with any Linux connector/driver... We ended up setting up a Windows server so that we could use the native SQL driver... then wrote an API in PHP so we could communicate to the server from any app/server protected with pub/priv keys. – Rob W Aug 04 '15 at 21:26
  • @HalfCrazed We've got it working just fine in our production environment at Rackspace. I am just trying to setup a new development environment and can't get it to work. – Jeremy H. Aug 05 '15 at 13:25
  • 1
    Did you solved your issue ? I'm stuck with the same and there are several posts here for the same issue with no working answer for me.. – Delphine Jul 28 '16 at 08:34
  • 1
    Take a look here http://stackoverflow.com/questions/37205752/sqlstate01002-adaptive-server-connection-failed-severity-9 and also subsequently here http://stackoverflow.com/questions/20163776/connect-php-to-mssql-via-pdo-odbc - this helped me. Basically I would look at setting the tds version to 8.0 as well as setting up the parameters and installing the odbc as required. But take a look at those links for more information in this regard. – Mystus Nov 24 '16 at 10:39

3 Answers3

1

Try removing the port from $hostname and adding it to freetds.conf.

[192.168.5.57]
    host = 192.168.5.57
    port = 1433
Jeffwa
  • 1,143
  • 10
  • 12
1

We got this error when trying to connect to a database that we weren't authorized to. Once we requested to be added to the whitelist (not sure whether it was at the firewall or db level), then we could connect okay.

Check that there's no restrictions by IP address.

Shane N
  • 1,742
  • 2
  • 17
  • 24
0

Correct your connection DSN according to PHP.net

"sqlsrv:Server=localhost;Database=testdb"

  • If someone is using dblib, then sqlsrv driver is not working for some reason, e.g. under most recent Debian 9. – Alexander Apr 03 '20 at 13:36