PDOStatement::closeCursor

PDOStatement::closeCursor β€” Closes the cursor, enabling the statement to 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 server, allowing other SQL statements to be issued, but leaves the statement in a state that enables it to be executed again.

This method is particularly useful for database drivers that do not support re-executing a PDOStatement object when there are still unfetched rows from the previous execution. If the database driver is subject to this limitation, out-of-sequence errors may occur.

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

<?php
do {
    while ($stmt->fetch()) ;
    if (!$stmt->nextRowset()) break;
} while (true);
?>

Return Values

Returns TRUE on success, or FALSE on failure.


Examples

An example of PDOStatement::closeCursor()

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 the application works correctly with all database drivers, PDOStatement::closeCursor() is called once 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 result set */
$stmt->fetch();

/* The following call to closeCursor() may be required by some drivers */
$stmt->closeCursor();

/* Now execute the second statement */
$otherStmt->execute();
?>