Re : Problem with PDO exceptions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




Message-ID: <49B2CE8F.1090500@xxxxxxxxxxxxx>
Date: Sat, 07 Mar 2009 20:44:15 +0100
From: Daniel Carrera <daniel.carrera@xxxxxxxxxxxxx>

Hello,

I have MySQL 5.1 and PHP 5.2. For some reason PDO is not throwing exceptions when I give it a broken SQL query. For example:

try {
        $stmt = $db->prepare("SELECT * FROM foobar WHERE 1");
} catch(PDOException $e) {
        error($e->getMessage());
}

In this example there is no table called 'foobar', so this should give an error. Yet, it doesn't.


When you create your DB connection $db, follow the connection line directly after with this :
$db->setAttribute(PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION);

The default is I believe PDO::ERRMODE_SILENT which is confusing to most people the first time.
http://uk2.php.net/manual/en/pdo.setattribute.php


HTH
Cheers - Neil


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux