To manipulate the error returning by a query you can use exceptions or function returns (errors).
The fourth argument is an array where some settings can be passed they enter the PDO::ATTR_ERRMODE
and the value PDO::ERRMODE_EXCEPTION
defines that errors shall be treated as exceptions.
To capture bank errors as exceptions first set this in PDO construction
$opcoes = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$db = new PDO('pgsql:host=localhost;dbname=test', 'root', '', $opcoes);
$sql = "select ... invalido";
$stmt = $db->prepare($sql);
try{
$stmt->execute();
}catch(PDOException $e){
echo $e->getMessage();
}
The standard approach is this, check the return of execute()
if false use the method errorInfo()
for details about the error, with the message and Sqlstate.
$db = new PDO('pgsql:host=localhost;dbname=test', 'root', '');
$sql = "select ... invalido";
$stmt = $db->prepare($sql);
if($stmt->execute() === false){
echo "<pre>";
print_r($stmt->errorInfo());
}
Yes it is possible! There are some ways to do this with exceptions or error returns.
– rray