使用PHP连接到sql server 2014

ff29svar  于 2022-11-21  发布在  PHP
关注(0)|答案(1)|浏览(181)

我正在尝试连接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

j0pj023g

j0pj023g1#

您需要安装PDO_SQLSRV驱动程序来访问数据库。您只安装了MySQL和SQLite的支持。
如果运行Linux服务器,则必须安装并使用ODBC

相关问题