To handle query errors (sql errors do not occur during extraction) and a more general PDO error, you should try using PDO::ERRMODE_EXCEPTION . Additionally, the PdoStatement returned by query() implements Traversable, so you can skip using fetch() for a simple query:
try { $db = new PDO('sqlite:mydb.db'); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Set Errorhandling to Exception foreach($db->query("SELECT...") as $row) { //Do domething } $db = null; // "Disconnect" } catch (PDOException $err) { //Handling query/error }
The call will be called if the request encounters an error (not fetch ()).
Fetch will always return false when it reaches the end of your result set.
grunk source share