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

38,789

Solution 1

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

Solution 2

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!";

Solution 3

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

Share:
38,789
hawx
Author by

hawx

Updated on July 09, 2022

Comments

  • hawx
    hawx almost 2 years

    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.

  • Matthew Fritz
    Matthew Fritz over 7 years
    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 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
  • Matthew Fritz
    Matthew Fritz over 7 years
    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.
  • Exceptional NullPointer
    Exceptional NullPointer about 4 years
    $pw = "secrets"; should be $pwd = "secrets";, this typo makes it produce only the severity error regardless of connection!