0
Using PDO and Postgres, but with difficulty to get the query Exception, for example error in syntax.
I’m grateful if anyone can shed some light!!
0
Using PDO and Postgres, but with difficulty to get the query Exception, for example error in syntax.
I’m grateful if anyone can shed some light!!
1
As you want to implement this in catch block, first thing is to change the PDO error control strategy, there are 3 strategies
By default it comes with PDO::ERRMODE_SILENT which reports errors via errorcode() and errorInfo(), which would be caught this way:
try {
$pdo = new DBClass();
} catch (PDOException $e) {
exit(1);
}
$sql = 'SELECT * FROM TABELA';
$stmt = $pdo->query($sql);
if (!$stmt) {
list($erro, $erro_driver, $msg_driver) = $pdo->errorInfo();
switch ($erro) {
case '42000':
//ERRO DE SINTAXE SEU TRATAMENTO AQUI...
exit(1);
...
}
}
errorInfo() returns an array of three positions;
As in your case you want to deal with the exceptions, we will have to change the PDO error control strategy as follows:
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
or the same code above would look like this:
try {
$pdo = new DBClass();
} catch (PDOException $e) {
exit(1);
}
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'SELECT * FROM TABELA';
try{
$stmt = $pdo->query($sql);
}catch(PDOException $e) {
switch ($e->getCode()) {
case '42000':
//ERRO DE SINTAXE SEU TRATAMENTO AQUI...
exit(1);
...
}
}
)
The Pdoexception class still has the methods
Great Nelson, my question was why he did not return getMessage() perfectly explained by you, I will put in practice here in the code
Browser other questions tagged php pdo
You are not signed in. Login or sign up in order to post.
I think that that answers, if you do not post your code in the question.
– rray
Can you put here the part of the code you already have? <br> Or even the error? It would be easier to analyze
– FábioArsénio
then, I want to implement the catch and return any error messages that there might be, as a non-existent column, errors that do not allow the query to run. Thank you!!!
– Christopher Tavares