user1535268
user1535268

Reputation: 131

PHP PDO dblib not working

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.

Upvotes: 3

Views: 12505

Answers (1)

Teena Thomas
Teena Thomas

Reputation: 5239

The PDO connection syntax is supposed to be like this,

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

Manual

Upvotes: 5

Related Questions