Conform to the php docs, 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 database access).
Let's suppose, I set the PHP error reporting system to throw exceptions on failure:
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
I need a case, a situation in which the fetch()
method will throw an exception. Why? Because I want to check, to be 100% sure that fetch()
throws an exception on failure, and doesn't just return FALSE
on failure.
If that would be the case, then I would indeed consider the FALSE
returned by fetch()
as the result of not finding any records in the db table.
So, my question would be: Do you know a way to simulate a failure situation for the fetch()
method?
Thank you.
P.S.: The answer to my question will help me find the answer for my other question: PHP PDO fetch returns FALSE when no records found AND on failure
Edit 1:
I also prepared an example, to show how I handle the exceptions. It's about a simple sql query, fetching a user from a users
table:
<?php
// Activate error reporting.
error_reporting(E_ALL);
ini_set('display_errors', 1);
try {
// Create a PDO instance as db connection to a MySQL db.
$connection = new PDO(
'mysql:host=localhost;port=3306;dbname=mydb;charset=utf8'
, 'myuser'
, 'mypass'
, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => FALSE,
PDO::ATTR_PERSISTENT => TRUE
)
);
// Define the sql statement.
$sql = 'SELECT * FROM users WHERE name = :name';
/*
* Prepare and validate the sql statement.
*
* --------------------------------------------------------------------------------
* If the database server cannot successfully prepare the statement, PDO::prepare()
* returns FALSE or emits PDOException (depending on error handling settings).
* --------------------------------------------------------------------------------
*/
$statement = $connection->prepare($sql);
if (!$statement) {
throw new UnexpectedValueException('The sql statement could not be prepared!');
}
// Bind the input parameter to the prepared statement.
$bound = $statement->bindValue(':name', 'Sarah', PDO::PARAM_STR);
// Validate the binding of the input parameter.
if (!$bound) {
throw new UnexpectedValueException('An input parameter can not be bound!');
}
/*
* Execute the prepared statement.
*
* ------------------------------------------------------------------
* PDOStatement::execute returns TRUE on success or FALSE on failure.
* ------------------------------------------------------------------
*/
$executed = $statement->execute();
if (!$executed) {
throw new UnexpectedValueException('The prepared statement can not be executed!');
}
/*
* Fetch and validate the result set.
*
* =========================================================
* Note:
* =========================================================
* PDOStatement::fetch returns FALSE not only on failure,
* but ALSO when no record is found!
*
* Instead, PDOStatement::fetchAll returns FALSE on failure,
* but an empty array if no record is found. This is the
* natural, desired behaviour.
* =========================================================
*/
$resultset = $statement->fetch(PDO::FETCH_ASSOC);
if ($resultset === FALSE) {
throw new UnexpectedValueException('Fetching data failed!');
}
// Display the result set.
var_dump($resultset);
echo '<pre>' . print_r($resultset, TRUE) . '</pre>';
// Close connection.
$connection = NULL;
} catch (PDOException $exc) {
echo '<pre>' . print_r($exc, TRUE) . '</pre>';
exit();
} catch (Exception $exc) {
echo '<pre>' . print_r($exc, TRUE) . '</pre>';
exit();
}
I used the following create table syntax:
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=124 DEFAULT CHARSET=utf8;
and the following table values:
INSERT INTO `users` (`id`, `name`)
VALUES
(1,'Sarah'),
(2,'John');
So, the table looks like this:
id name
--------
1 Sarah
2 John