PHP PDOStatement:: closeCursor Explanation

  • 2021-11-14 05:12:10
  • OfStack

PDOStatement::closeCursor

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

Description

Grammar


bool PDOStatement::closeCursor ( void )

PDOStatement::closeCursor() Release the connection to the database service to issue other SQL statements, but leave the statement in a state where it can be executed again.

This method is useful for database drivers that do not support the execution of an additional PDOStatement object when the last PDOStatement object still has unfetched rows. If the database driver is limited by this, the problem of out-of-order error may occur.

PDOStatement::closeCursor() Either it is implemented by the unique method of an optional driver (the most efficient), or it is implemented as a 1-like PDO backup when there is no driver specific function. 1-like alternate semantics are the same as the following PHP code:


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

Return value

Returns TRUE on success or FALSE on failure.

Instances

1 example of PDOStatement:: closeCursor ()

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 with no rows. To ensure that the application works properly for all database drivers, call $stmt once before executing the $otherStmt PDOStatement object PDOStatement::closeCursor() .


<?php
/*  Create 1 A  PDOStatement  Object  */
$stmt = $dbh->prepare('SELECT foo FROM bar');
/*  Create the 2 A  PDOStatement  Object  */
$otherStmt = $dbh->prepare('SELECT foobaz FROM foobar');
/*  Executive part 1 Bar statement  */
$stmt->execute();
/*  Extract only the number from the result set 1 Row  */
$stmt->fetch();
/* The following call to closeCursor() may be required by some drivers */
$stmt->closeCursor();
/*  You can now execute the 2 Article statement  */
$otherStmt->execute();
?>

Summarize


Related articles: