NotGaeL
NotGaeL

Reputation: 8484

PDO Uncaught PDO exception dumps password on connection error even with catch block

I have this block:

    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);
        }

If, for example, I shutdown mysql, I get this on error_log when executing the code:

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/

Upvotes: 0

Views: 373

Answers (1)

NotGaeL
NotGaeL

Reputation: 8484

As @Jon said:

@elcodedocle: It's definitely the namespace if you have not imported PDOException. catch (\PDOException $e) instead and it should work.

(An obvious issue but a little confusing output: "Undefined exception" would be much more helpful to debug this...)

Upvotes: 1

Related Questions