I am writing an installer for one of my apps and I would like to be able to test some default database settings.
Is this possible using PDO to test valid and invalid
As seen e.g. in the comments at this answer (but hardly anywhere else, so I made it more visible here), the "classic" PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
solution does not always work.
The implementation of PDO::ERRMODE_EXCEPTION
is broken, so it seems to be "leaking" in some cases.
For example:
Warning: PDO::__construct() [pdo.--construct]: [2002] No connection could be made because the target machine actively refused it. (trying to connect via tcp://localhost:3306) in [...] db.php on line 34
The code there:
try {
$this->pdo = new PDO($cfg['DB'], $cfg['DB_USER'], $cfg['DB_PASS'],
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
} catch {
echo("Can't open the database.");
}
The exception is thrown (and cought: I can see my message).
So, as a necessary workaround, you need to also put a @
(let's call it a "diaper operator" in this case) before new pdo(...)
to actually keep it clean.