PDO: error handling

I work with PDO in PHP.

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

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

"Something went wrong:" . the_error

Or do you need to always do this every time every $ sql?

+4
source share
2 answers

You can do this using PDO::errorInfo()

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

It is about as global as you are going to get.

+3
source

You can always catch the exceptions thrown by the PDO class.

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

All Articles