Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PDOStatement::closeCursor

PDOStatement::closeCursor

PHP PDO Reference ManualPHP PDO Reference Manual

PDOStatement::closeCursor - Close the cursor so the statement can be executed again. (PHP 5 >= 5.1.0, PECL pdo >= 0.9.0)


Description

Syntax



bool PDOStatement::closeCursor ( void )

PDOStatement::closeCursor() releases the connection to the database service so that other SQL statements can be issued, while leaving the statement in a state where it can be executed again.

This method is useful for database drivers that do not support executing another PDOStatement object while the previous executed PDOStatement object still has unfetched rows. If a database driver is subject to this limitation, out-of-order errors may occur.

PDOStatement::closeCursor() is implemented either as an optional driver-specific method (most efficient) or as a general PDO fallback when no driver-specific functionality exists. The general fallback is semantically equivalent to the following PHP code:



fetch())

        ;

    if (!$stmt->nextRowset())

        break;

} while (true);

?>


Return Value

Returns TRUE on success, or FALSE on failure.


Example

An example of PDOStatement::closeCursor()

In the example below, 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 the application runs correctly on all database drivers, $stmt calls PDOStatement::closeCursor() once before executing the $otherStmt PDOStatement object.



prepare('SELECT foo FROM bar');



/* Create a second PDOStatement object */

$otherStmt = $dbh->prepare('SELECT foobaz FROM foobar');



/* Execute the first statement */

$stmt->execute();



/* Only retrieve the first row from the result set */

$stmt->fetch();



/* The following call to closeCursor() may be required by some drivers */

$stmt->closeCursor();



/* Now the second statement can be executed */

$otherStmt->execute();

?>


PHP PDO Reference ManualPHP PDO Reference Manual