5

I have the following script to connect to my microsoft azure server.

<?php

try {
    $hostname = "secrets.database.windows.net";
    $dbname = "secrets";
    $username = "secrets";
    $pw = "secrets";
    $dbh = new PDO ("dblib:host=$hostname;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
}

echo "Passed!";

The script above passes on my old server, but gives me the following error message when executed from new server.

SQLSTATE[01002] Adaptive Server connection failed (severity 9)

My new server PHP setup is as follows:

sudo apt-get install -y php5.6-fpm php5.6-ldap php5.6-curl php5.6-cli   php5.6-mcrypt php5.6-intl php5.6-json php5.6-pdo-dblib php5.6-mysqlnd php5.6-memcached php5.6-mbstring php5.6-imap php5.6-xml php5.6-sybase

My checks so far:

1) Both have same public facing IP address.

2) Both have identical PHP PDO/ODBC setup.

$ php -i | grep PDO
DO
PDO support => enabled
PDO drivers => dblib, mysql, odbc
PDO Driver for FreeTDS/Sybase DB-lib => enabled
PDO Driver for MySQL => enabled
PDO_ODBC
PDO Driver for ODBC (unixODBC) => enabled

3) I am able to ping my server using telnet from both servers using:

telnet secrets.database.windows.net 1433

Any suggestions would be appreciated.

hawx
  • 1,629
  • 5
  • 21
  • 37

5 Answers5

12

After some further googling I came across this answer.

Connect PHP to MSSQL via PDO ODBC

Turned out I just needed to update my /etc/freetds/freetds.conf

My changes:

Uncommented TDS protocol version and updated.

tds version = 8.0

Added mssql below examples.

[mssql]
host =
Port = 1433
tds version = 8.0
Community
  • 1
  • 1
hawx
  • 1,629
  • 5
  • 21
  • 37
  • 1
    I had the exact same issue. Following the answer in the link you provided certainly assisted. My only concern was that tds version 8.0 actually falls back to tds version 7.1 as described here http://www.freetds.org/userguide/choosingtdsprotocol.htm however it still looks more stable than when I was using 7.2 which would give me the adaptive server connections on an adhoc basis – Mystus Nov 24 '16 at 10:35
  • 1
    Considering this I would also go ahead and mark your answer as correct. If I had asked the question myself personally, your answer would be applicable. – Mystus Nov 24 '16 at 10:36
6

I got the same issue here, but it is fixed by adding version of FreeTDS 8.0 to the connection directly in PHP code:

<?php

try {
    $hostname = "secrets.database.windows.net";
    $dbname = "secrets";
    $username = "secrets";
    $pw = "secrets";
    $dbh = new PDO ("dblib:version=8.0;charset=UTF-8;host={$hostname};dbname={$dbname}", $username, $pwd);
} catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
}

echo "Passed!";
Kannika
  • 2,538
  • 2
  • 27
  • 38
1

In my case I had a typo in $dbname, correcting it solved the issue.

redacted
  • 3,789
  • 6
  • 25
  • 38
0

In my case the user's password had been reset without my knowledge.

grizzb
  • 329
  • 5
  • 8
0

In my case it was because the whl package you install when you launch "pip install pymssql" onboards the freetds 1.00.97. Rebuilding the package from source with freetds 1.1.6 installed did the trick and permit me to connect to the server.

Gourgandine
  • 189
  • 6