即使使用catch块,PDO Uncaught PDO异常也会在连接错误时转储密码

时间:2014-01-08 12:36:19

标签: php mysql pdo exception-handling

我有这个块:

    try {
            $this->dbh = new PDO(
                "mysql:host=".appparams::dBHost.
                ";port=".appparams::dBPort.
                ";dbname=".appparams::dBName.
                ";charset=utf8",
                appparams::dBUser,
                appparams::dBPassword, 
                array(
                    PDO::ATTR_PERSISTENT => true,
                    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'",
                    PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
                )
            );
            $this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); #line 36
            $this->dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        } catch (PDOException $e) {
            trigger_error("501", E_USER_ERROR);
        }

例如,如果我关闭mysql,我在执行代码时会在error_log上得到这个:

PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /var/www/myweb/mypage/db.php:36\nStack trace:
#0 /var/www/myweb/mypage/db.php(36): PDO->__construct('mysql:host=loca...', 'root', 'my_database_password!!!', Array)
#1 /var/www/myweb/mypage/accesslogmapcontroller.php(84): myweb\\db->__construct()
#2 /var/www/myweb/mypage/main.php(54): myweb\\accesslogmapcontroller::process('READ', Array)
#3 /var/www/myweb/mypage/main.php(179): myweb\\main::main()
#4 {main}\n  thrown in /var/www/myweb/mypage/db.php on line 36, referer: http://mydomain.com/myweb/mypage/

1 个答案:

答案 0 :(得分:1)

正如@Jon所说:

@elcodedocle:如果你没有导入PDOException,那肯定是命名空间。 catch(\ PDOException $ e)代替它应该工作。

(一个明显的问题,但输出有点令人困惑:“未定义的异常”对调试这个更有帮助...)