PDOStatement::closeCursor — Closes the cursor so that the statement can be executed again. (PHP 5 >= 5.1.0, PECL pdo >= 0.9.0)
bool PDOStatement::closeCursor ( void )
PDOStatement::closeCursor() releases the connection to the database service so that other SQL statements can be issued, but leaves the statement in a state where it can be executed again.
This method is very useful for database drivers that do not support executing another PDOStatement object when there are still unfetched rows in the last executed PDOStatement object. If the database driver is subject to this limitation, out-of-order errors may occur.
PDOStatement::closeCursor() is either implemented as an optional driver-specific method (the most efficient), or implemented as a general PDO fallback when there is no driver-specific functionality. The general fallback semantics are the same as the following PHP code:
<?phpdo { while ($stmt->fetch()) ; if (!$stmt->nextRowset()) break;} while (true);?>
Returns TRUE on success, or FALSE on failure.
In the following example, the $stmt PDOStatement object returns multiple rows, but the application only fetches the first row, leaving the PDOStatement object in a state with unfetched rows. To ensure that the application can run normally with all database drivers, $stmt calls PDOStatement::closeCursor() once 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 result set */$stmt->fetch();/* The following call to closeCursor() may be required by some drivers */$stmt->closeCursor();/* You can now execute the second Statement*/$otherStmt->execute();?>