I created the following method to execute my querys:
public function setQuery($query) {
try {
$stmt = self::$conn->prepare($this->limpaQuery($query));
return $stmt->execute();
} catch (PDOException $Exception) {
var_export($query);
die();
}
}
It works perfectly, but when I run a wrong query it has the following error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE [42S22]: Column not found: 1054 Unknown column ...
What I do not understand is why it does not get PDOException, I wanted every query that it did not execute to be displayed.
Updade
/**
* Conexao
*/
public function openConection() {
try {
//Verifica se uma instancia já existe
if (!isset(self::$conn)) {
//String de conexão
self::$conn = new PDO("mysql:host=$this->servername;dbname=$this->banco", $this->username, $this->password);
// set the PDO error mode to exception
self::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
//Mensagem de status
$this->status = "Conectado no host $this->servername, no banco dbname=$this->banco com o usuário $this->username";
} catch (PDOException $e) {
//Mensagem de status
$this->status = "Falha ao tentar conectar: \n" . $e->getMessage();
}
}