PHP:无法找到驱动程序(PDO, MySql)

时间:2021-09-24 07:09:34
<?php

error_reporting( E_ALL & ~E_DEPRECATED & ~E_NOTICE );
ob_start();
session_start();

define('DB_DRIVER', 'mysql');
define('DB_SERVER', 'localhost');
define('DB_SERVER_USERNAME', 'root');
define('DB_SERVER_PASSWORD', '');
define('DB_DATABASE', 'db_test');

define('PROJECT_NAME', 'Testing create');
$dboptions = array(
        PDO::ATTR_PERSISTENT => FALSE,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAME utf8',
    );

try{
    $DB = new PDO(DB_SERVER. ':host='.DB_SERVER. ';dbname='.DB_DATABASE,DB_SERVER_USERNAME, DB_SERVER_PASSWORD , $dboptions);
}catch (Exception $ex) {
    echo $ex->getMessage();
    die;
}

require_once 'functions.php';

if($_SESSION["errorType"] != "" && $_SESSION["errorMsg"] != "" ){
    $ERROR_TYPE = $_SESSION["errorType"];
    $ERROR_MSG = $_SESSION["errorMsg"];
    $_SESSION["errorType"] = "";
    $_SESSION["errorMsg"] = "";
}

?>

The code above give error "could not find driver". I'm following this tutorial to create them, downloaded their source code and view on browser, no error at all. But when im trying to create my program based on their, i got the error. FYI my PDO extension is enable, checked it twice.

上面的代码错误“找不到驱动程序”。我正在跟随本教程创建他们,下载他们的源代码和浏览浏览器,没有任何错误。但是当我尝试基于他们的程序创建我的程序时,我得到了错误。我的PDO扩展是允许的,检查了两次。

What is wrong with my code?

我的代码有什么问题?

Thank you in advance.

提前谢谢你。

1 个解决方案

#1


4  

Correct the $DB to:

正确的$ DB:

$DB = new PDO(DB_DRIVER. ':host='.DB_SERVER. ';dbname='.DB_DATABASE,DB_SERVER_USERNAME, DB_SERVER_PASSWORD , $dboptions);

Here is sample that easier to understand

这里有更容易理解的样本。

$db = new PDO('dblib:host=your_hostname;dbname=your_db;charset=UTF-8', $user, $pass);

#1


4  

Correct the $DB to:

正确的$ DB:

$DB = new PDO(DB_DRIVER. ':host='.DB_SERVER. ';dbname='.DB_DATABASE,DB_SERVER_USERNAME, DB_SERVER_PASSWORD , $dboptions);

Here is sample that easier to understand

这里有更容易理解的样本。

$db = new PDO('dblib:host=your_hostname;dbname=your_db;charset=UTF-8', $user, $pass);