开发者

PHP: Custom error handler for PDO?

开发者 https://www.devze.com 2023-01-19 11:34 出处:网络
I am attempting to try to do something useful with PDO exceptions other than display them, but I cannot find for the life of me how to use an error handler (set_error_handler) or anything custom to de

I am attempting to try to do something useful with PDO exceptions other than display them, but I cannot find for the life of me how to use an error handler (set_error_handler) or anything custom to deal with PDOs exceptions.

Now I am using try..catch blocks of course the catch the exception, do I implement a custom error handler in the catch part, or can I skip the try->catch block completely as the exception handler would handle it for me (by calling or throw new exception (...) on the statement?

I guess what I am asking is an examp开发者_JAVA百科le to catch PDO exceptions and log them more or less (any simple code correct or not I can use, I'm not TOO dumb).


You'll have to use a try..catch block around each PDO query. Add your log function in the catch part. There is no generic handler for exceptions in PHP.

try {  
   pdo::error();  
}  
catch (Exception $e) {  
   syslog($e);  
}  

If you want to avoid the try..catch blocks you can however configure PDO to only show errors instead of throwing exceptions. http://php.net/manual/en/pdo.error-handling.php

 $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号