PHP PDO dblib not working

12,260

The PDO connection syntax is supposed to be like this,

$DBH = new PDO("dblib:host=$myServer;dbname=$myDB", $myUser, $myPass); //wrongly placed quotes

Manual

Share:
12,260

Related videos on Youtube

user1535268
Author by

user1535268

Updated on September 15, 2022

Comments

  • user1535268
    user1535268 over 1 year

    I am trying to connect to MS SQL database which is hosted on a different set of servers. I can connect the old way.

    $myServer = "server name";
    $myUser = "username";
    $myPass = "pword";
    $myDB = "dbname"; 
    
    if (is_callable('mssql_connect')) {
        $link = mssql_connect($myServer, $myUser, $myPass);
    
        if (!$link) {
            die('connection failed');
        }   
    } else {
        echo 'mssql_connect() is not supported on this environment';
    }
    
    //connection to the database
    $dbhandle = mssql_connect($myServer, $myUser, $myPass);
    
    //select a database to work with
    $selected = mssql_select_db($myDB, $dbhandle)
      or die("Couldn't open database $myDB"); 
    

    That works just fine to connect to the ms sql server. When I try with PDO dblib I get the following error "SQLSTATE[01002] Adaptive Server connection failed (severity 9)"

    Here is the code I am using for that and I have checked and pdo is installed with dblib as an option to use. Credentials are all exactly the same.

    $myServer = "server name";
    $myUser = "username";
    $myPass = "pword";
    $myDB = "dbname"; 
    
    try {
      # MS SQL Server and Sybase with PDO_DBLIB
      $DBH = new PDO("dblib:host=$myServer;dbname=$myDB, $myUser, $myPass");
    }
    catch(PDOException $e) {
        echo $e->getMessage();
    }
    

    Any help would be appreciated as I am planning to create a new application in php to connect to this MS SQL db but in the future plan to migrate db over to Mysql once all of the old classic asp's are rebuilt into php.

  • user1535268
    user1535268 over 11 years
    That worked like a charm. Thanks for saving me more frustration.