我正在尝试连接SQL Server 2014中的数据库与PHP如下:
首先,我在conexaosql.php中有这个脚本:
class Conexao
{
private static $connection;
private function __construct(){}
public static function getConnection() {
$pdoConfig = DB_DRIVER . ":". "Server=" . DB_HOST . ";";
$pdoConfig .= "Database=".DB_NAME.";";
try {
if(!isset($connection)){
$connection = new PDO($pdoConfig, DB_USER, DB_PASSWORD);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return $connection;
} catch (PDOException $e) {
$mensagem = "Drivers disponiveis: " . implode(",", PDO::getAvailableDrivers());
$mensagem .= "\nErro: " . $e->getMessage();
throw new Exception($mensagem);
}
}
}
然后我调用这个脚本,得到下面的代码:
define('DB_HOST' , "xxxx");
define('DB_USER' , "xxxx");
define('DB_PASSWORD' , "xxxx");
define('DB_NAME' , "xxxx");
define('DB_DRIVER' , "sqlsrv");
require ("conexaosql.php");
try{
$Conexao = Conexao::getConnection();
$query = $Conexao->query("SELECT Pago FROM UTE02.dbo.Recibos");
$produtos = $query->fetchAll();
}catch(Exception $e){
echo $e->getMessage();
exit;
}
运行代码时出现以下错误:
mysql、sqlite错误:找不到驱动程序
我使用的是php8.1和apache。我用phpinfo()中的图片离开了link。
1条答案
按热度按时间j0pj023g1#
您需要安装PDO_SQLSRV驱动程序来访问数据库。您只安装了MySQL和SQLite的支持。
如果运行Linux服务器,则必须安装并使用ODBC。