I am new to PDO and a few things about it confuse me, I’ve tried creating a test function to see whether an exception will be thrown for an invalid query but nothing is thrown.
here is the code
<?php
include_once("/var/www/include/constants.php");
class DB{
private $DBH;
public function DB(){
try{
$DBH = new PDO("mysql:host=".DB_SERVER.";dbname=".DB_NAME, DB_USER, DB_PASS);
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
public function test(){
try{
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$DBH->prepare('DELECT id FROM users');
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
};
/* Create database connection */
$db = new DB;
$db->test();
?>
Besides the missing references to the
$thisof your database handle, your need to tell PDO that it must not emulate prepares. The code below will throw a exception like this: