The PDO method fetch() returns the value FALSE
both when no records are found AND on failure (e.g. when something goes wrong regarding the data
This is what PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
is for. Use it like this:
$pdo = new PDO(
'mysql:host=localhost;port=3306;dbname=mydb;charset=utf8'
, 'user'
, 'pass'
, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ]
);
When used this way errors actually get thrown as exceptions. This means that should an error occur with fetch (or other methods using this pdo object) an exception will be thrown and the method won't actually return at all. This is a very effective way of handling errors in PDO. Now you know that if fetch returns a value no errors occured and therefore if it is false then the query returned no records.