我正在尝试连接到托管在不同服务器集上的MS SQL数据库。我可以用旧的方式连接。
$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");
可以正常连接到ms sql server。当我尝试使用PDO dblib时,我收到以下错误 “SQLSTATE [01002] Adaptive Server连接失败(严重级9)”
以下是我正在使用的代码,我已经检查过,并且pdo安装了dblib作为选项使用。凭证都完全相同。
$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();
}
任何帮助都会受到赞赏,因为我计划在php中创建一个新的应用程序以连接到这个MS SQL数据库,但是在将来所有旧的经典asp重建为php后,计划将db迁移到Mysql。 / p>
答案 0 :(得分:5)
PDO连接语法应该是这样的,
$DBH = new PDO("dblib:host=$myServer;dbname=$myDB", $myUser, $myPass); //wrongly placed quotes