In the following example, the $stmt
PDOStatement
object returns multiple rows but the application fetches only the first
row, leaving the PDOStatement object in a state of having unfetched rows.
To ensure that the application will work with all database drivers, the
author inserts a call to PDOStatement->closeCursor()
on $stmt
before executing the
$otherStmt
PDOStatement object.
<?php
/* Create a PDOStatement object */
$stmt = $dbh->prepare('SELECT foo FROM bar');
/* Create a second PDOStatement object */
$otherStmt = $dbh->prepare('SELECT foobaz FROM foobar');
/* Execute the first statement */
$stmt->execute();
/* Fetch only the first row from the results */
$stmt->fetch();
/* The following call to closeCursor() may be required by some drivers */
$stmt->closeCursor();
/* Now we can execute the second statement */
$otherStmt->execute();
?>