ShafiqqAziz
ShafiqqAziz

Reputation: 71

PHP : could not find driver (PDO, MySql)

<?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.

What is wrong with my code?

Thank you in advance.

Upvotes: 1

Views: 4243

Answers (1)

BQ Kh&#225;nh
BQ Kh&#225;nh

Reputation: 96

Correct the $DB to:

$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);

Upvotes: 4

Related Questions