Karem
Karem

Reputation: 18123

PDO: error handling

I am working with PDO in PHP.

Now I wonder if you could catch any global error and show.

With global I mean if any $sql=$connect->prepare() fails, then echo out

"Something went wrong:" . the_error

Or would you need to always do it invidually each $sql ?

Upvotes: 4

Views: 2581

Answers (2)

Robin Orheden
Robin Orheden

Reputation: 2764

You could always catch exceptions thrown by the PDO class.

try
{
    ...new PDO('odbc:SAMPLE', 'db2inst1',...
}
catch(PDOException $exception)
{
    echo "Failed: " . $exception->getMessage();
}

Upvotes: 3

Galen
Galen

Reputation: 30170

You can do it using PDO::errorInfo()

http://www.php.net/manual/en/pdo.errorinfo.php

That's about as global as youre going to get.

Upvotes: 3

Related Questions