简体   繁体   中英

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.

The PDO connection syntax is supposed to be like this,

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

Manual

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM