I'm connecting to a mySQL database using PDO. When the login is wrong it gives a whole stacktrace of errors, even though I used try-catch.
try
{
$this->db = new PDO("mysql:host='host' dbname='db' charset=utf8", $username, $password);
// Tried both with and without these attributes
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch(PDOException $e)
{
echo "Database connection error: " . $e->getMessage());
exit;
}
When this code is run - with a non-existing database name - I get the following error:
Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1044] Access denied for user 'user' to database 'db' in .....
It prints out all the information. In case there's some error in the login, I just want the code to exit and write a message to a log file.
Why does the catch not catch this exception?
I'm using a local Apache server on my Windows computer. Maybe this is caused by some mis-configuration?
Any help is appreciated.