Content · Web Development · PHP

PHP PDOStatement::closeCursor() Method: Usage and Examples

Releases any remaining statement rows so the database connection can issue another statement. It does not close the PDO connection or delete the prepared

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Releases any remaining statement rows so the database connection can issue another statement.

The objective is to understand the behavior and operational limits, not only the method name.

What It Does


Releases any remaining statement rows so the database connection can issue another statement.

Syntax and Return Value


PDOStatement::closeCursor(): mixed

The return value and exceptional cases depend on the call described here: It does not close the PDO connection or delete the prepared statement.

Example 1: Basic Use


The statement has a real query and result set; database-specific metadata and statement options may be unsupported.

<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$stmt = $pdo->query("SELECT name FROM products ORDER BY id");
$stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor();
echo $pdo->query("SELECT COUNT(*) FROM products")->fetchColumn();

Example 2: A Second Scenario


The statement has a real query and result set; database-specific metadata and statement options may be unsupported. This additional check distinguishes the method's result from connection or statement state.

<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$stmt = $pdo->query("SELECT name FROM products ORDER BY id");
$stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor();
echo $pdo->query("SELECT COUNT(*) FROM products")->fetchColumn();
echo $stmt->closeCursor() ? "cursor closed" : "close failed";

Example 3: Practical Checks


The statement has a real query and result set; database-specific metadata and statement options may be unsupported. This additional check distinguishes the method's result from connection or statement state.

<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$stmt = $pdo->query("SELECT name FROM products ORDER BY id");
$stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor();
echo $pdo->query("SELECT COUNT(*) FROM products")->fetchColumn();
echo "After closing, prepare next query: " . $pdo->query("SELECT 1")->fetchColumn();

Important Behavior and Edge Cases


It does not close the PDO connection or delete the prepared statement.

For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.

Comparison with Related Approaches


ApproachImportant distinction
PDOStatement::closeCursorReleases any remaining statement rows so the database connection can issue another statement.
Related API / patternfetchAll loads remaining rows into PHP memory instead of just releasing them.

Common Beginner Mistakes


MistakeCorrect approach
Assuming the happy path covers edge casesIt does not close the PDO connection or delete the prepared statement.
Using a related API interchangeablyfetchAll loads remaining rows into PHP memory instead of just releasing them.

Key Takeaways


  • Releases any remaining statement rows so the database connection can issue another statement.
  • It does not close the PDO connection or delete the prepared statement.
  • fetchAll loads remaining rows into PHP memory instead of just releasing them.

Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.

Share this resourceSend this page to someone who may find it useful.