我使用以下代码尝试连接:
try {
$hostname = "***********";
$port = *****;
$dbname = "******";
$username = "******";
$pw = "********";
$dbh = new PDO ("sqlsrv:host=$hostname:$port;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
但我收到以下错误:
Failed to get DB handle: could not find driver
如何解决此错误?
我已经看到其他答案说我应该有" SQL Server PDO驱动程序"安装。有没有办法检查这是否已安装?
答案 0 :(得分:0)
如果你运行php的操作系统是Windows,那么你必须按照https://technet.microsoft.com/en-us/library/cc793139(v=sql.90).aspx所述设置SQL Server php扩展。如果你在Linux上运行PHP,那么你必须安装扩展php-mssql或odbc驱动程序:
PHP和在Unix / Linux中访问MS SQL Server: http://www.codeproject.com/Tips/607814/PHP-and-Accessing-MS-SQL-Server-in-Unix-Linux
使用PHP远程连接到MSSQL: https://www.centos.org/forums/viewtopic.php?t=21567
同时检查此相关问题并回答: Connecting to mssql using pdo through php and linux