Jeremy H.
Jeremy H.

Reputation: 523

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

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();
}

Upvotes: 3

Views: 21519

Answers (3)

Konstantin Scheumann
Konstantin Scheumann

Reputation: 23

Correct your connection DSN according to PHP.net

"sqlsrv:Server=localhost;Database=testdb"

Upvotes: 0

Shane N
Shane N

Reputation: 1741

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.

Upvotes: 1

Jeffwa
Jeffwa

Reputation: 1143

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

[192.168.5.57]
    host = 192.168.5.57
    port = 1433

Upvotes: 1

Related Questions