I love PHP's PDO (PHP Data Objects) extension; it gives a consistent, object-oriented interface to handling all kinds of relational database backends. One thing that annoys me is that the MySQL driver for PDO defaults to a silent error mode which can make SQL errors tricky to spot!
To give you an example, consider the query below (the correct
tablename is country
, so this SQL will fail):
$db = new PDO('mysql:host=localhost;dbname=sakila', 'user', 'pass'); $sql = 'select * from countrt'; $stmt = $db->query($sql); while(($row = $stmt->fetch()) != false) { echo $row['country'] . "\n"; }
The script will output an error because $stmt
is not
an object.
You have a few options here - you can check that you got an
object back before you try to do anything with it, for example.
Alternatively you can prepare()
and then …