PDO: error handling

2020-07-17 14:57发布

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 ?

标签: php pdo
2条回答
Ridiculous、
2楼-- · 2020-07-17 15:15

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.

查看更多
▲ chillily
3楼-- · 2020-07-17 15:40

You could always catch exceptions thrown by the PDO class.

try
{
    ...new PDO('odbc:SAMPLE', 'db2inst1',...
}
catch(PDOException $exception)
{
    echo "Failed: " . $exception->getMessage();
}
查看更多
登录 后发表回答