when i am fetching a database table from PDO fetch then it shows bool(false)
try {
$stmt = $db->prepare('SELECT * FROM replies WHERE comment_id = :comment_id');
$stmt->execute(array(':comment_id' => $postcomment['id']));
$repl = $stmt->fetch();
} catch (Exception $e) {
echo 'Error: '.$e->getMessage();
}
var_dump($repl); ?>
You appear to be expecting an exception when the query fails. That only happens AFAIK when you set the appropriate attribute on the PDO object (either PDOStatement
or PDO
, see PDO::setAttribute
):
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
after setting this, all fetch calls (and similar) will throw an exception on error instead of returning some false
return value. Then, the try-catch block actually provides some benefits.
Remark: The setAttribute
stuff can also be done in the PDO constructor (see PDO::__construct
)