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

1 Answer 1

4

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);
Sign up to request clarification or add additional context in comments.

3 Comments

successfully remove the error. but another problem appear, my page connected to this connection load like forever. do i need to change the whole codes?
you can try to remove this line "require_once 'functions.php';". Does it still happen?
so far it's going smoothly, i'll accept yours as answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.