3
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 shows the following error:
Fatal error: Uncaught Exception 'Pdoexception' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column...
What I don’t understand is why it doesn’t take Pdoexception, I wanted every query that doesn’t run to be displayed.
Height
/**
* 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();
}
}
Are you sure the exception is triggered in this method?
– Woss
Yes, he speaks exactly this line, and I put the Try catch in all the methods that run querys.
– Wictor Chaves
That answer might help you What Try/Catch Blocks are for and when they should be used?
– DNick