Saturday, 3 June 2017

php - PDO not catching error when I SELECT a table that doesn't exist

I am trying to try/catch a PDO::query to make sure whether a table exists or not. But Instead of catching the error and returning true or false it is just throwing an uncaught error:



public function __construct(){
$this->db = parent::connect();
}


public function table_exists($table){
try {
$this->query("SELECT 1 FROM `$table` LIMIT 1");
return true;
} catch (Exception $e) {
return false;
}
}

public function query($sql){

try {
$this->result = $this->db->query($sql);
return $this;
} catch (Exception $e) {
return null;
}
}


I also tried this $this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); inside the try's but it didn't make any difference. So I get





Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[42S02]: Base table or view not found: 1146 Table
'refritec.user' doesn't exist' in
...\DatabaseDriverMysql.php:34 Stack trace:
0 ...\DatabaseDriverMysql.php(34): PDO->query('SELECT 1 FROM ...') 1
...\DatabaseDriverMysql.php(25):
Core\DatabaseDriver->query('SELECT 1 FROM `...') 2
...\Pen.php(123):

Core\DatabaseDriver->table_exists('user') #3 [internal function]:
Core\Pen->update() #4 ...\index.php(97):
call_user_func_array(Array, Array) #5 {main} thrown in
...\DatabaseDriverMysql.php on line 34


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...