PHP error guide

MySQL commands out of sync: meaning and fix

Error summary

Fetch all needed rows or call closeCursor() before issuing another statement. A second statement runs before an unbuffered result set is consumed or closed.

What it means

Fetch all needed rows or call closeCursor() before issuing another statement. A second statement runs before an unbuffered result set is consumed or closed.

What the error means

This message means that a second statement runs before an unbuffered result set is consumed or closed. The exact signature distinguishes mysql commands out of sync from a generic application failure.

Why PHP produces it

The engine or service reports “MySQL commands out of sync” because its required precondition was not met. Fetch all needed rows or call closeCursor() before issuing another statement.

PHP version notes

The mysql commands out of sync wording here is based on PDO with MySQL/MariaDB; exact server wording varies; punctuation and exception class names can differ on older branches or vendor builds.

Most common causes

  • The immediate input or configuration reaches the specific condition: a second statement runs before an unbuffered result set is consumed or closed
  • The code path assumes the prerequisite for mysql commands out of sync has already been satisfied.
  • For mysql commands out of sync, development and production differ in version, extension, permissions, paths, or service configuration.
  • An earlier operation returned an unchecked value that is consumed by the line reporting mysql commands out of sync.

Minimal examples

BAD — reproduces the problem

php
$a = $pdo->query($sql);
$b = $pdo->query($other);

FIXED — safer pattern

php
$a = $pdo->query($sql);
while ($row = $a->fetch()) { process($row); }
$a->closeCursor();

Step-by-step diagnosis

  1. Copy the complete “SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active” text and retain the first application stack frame.
  2. Reproduce mysql commands out of sync in the same SAPI and environment listed for this page.
  3. Before changing mysql commands out of sync, inspect the preceding value or directive and verify its type, path, version, and permissions.
  4. Apply the narrow correction—fetch all needed rows or call closeCursor() before issuing another statement—then repeat the original request once.

Fixes

Correct the failing prerequisite

Fetch all needed rows or call closeCursor() before issuing another statement

php
$a = $pdo->query($sql);
while ($row = $a->fetch()) { process($row); }
$a->closeCursor();

Fail explicitly at the boundary

Validate the condition before the operation that emits mysql commands out of sync, and log a safe diagnostic without credentials or full production paths.

Common mistakes when fixing it

  • Suppressing mysql commands out of sync instead of correcting its upstream condition.
  • Testing mysql commands out of sync only with the CLI binary when the failing request runs under FPM or Apache.
  • Changing a global setting for mysql commands out of sync before confirming the site-specific effective configuration.

How to prevent it

  • Add a focused test that exercises the boundary responsible for mysql commands out of sync.
  • Keep runtime versions, extensions, configuration, and deploy artifacts affecting mysql commands out of sync reproducible.
  • Validate external data and service return values before they can trigger mysql commands out of sync.

Web server / environment notes

fpm, cli, docker, linux. The failure occurs where a second statement runs before an unbuffered result set is consumed or closed

Tags: fpm,cli,docker,linux

Categories