Skip to content

Rebind PDO statement parameters on every execute - #234

Open
simon-mundy wants to merge 3 commits into
php-db:0.6.xfrom
simon-mundy:fix/pdo-statement-rebind-parameters
Open

simon-mundy wants to merge 3 commits into
php-db:0.6.xfrom
simon-mundy:fix/pdo-statement-rebind-parameters

Conversation

@simon-mundy

Copy link
Copy Markdown
Member

Problem

Pdo\Statement::bindParametersFromContainer() returned early once $parametersBound was set, so re-executing a prepared statement with new values silently ran with the first values:

$statement->setSql('SELECT name FROM t WHERE id = :id');
$statement->execute(['id' => 1]); // 'one'
$statement->execute(['id' => 2]); // 'one' — expected 'two'

The same happened when passing a replacement ParameterContainer or mutating the existing container between executes. PDO's bindParam() binds by reference to a local copy of the container's values, so later container changes never reached the statement.

Fix

Remove the early return so parameters are bound from the current container on every execute(). The $parametersBound property is left in place for subclass compatibility.

Tests

  • Three regression tests (array params, replacement container, mutated container) against an in-memory SQLite table; all failed before the fix.
  • Removed two tests whose names described the old skip-when-bound behaviour; the new tests cover that path with real assertions.
  • Pdo\Statement stays at 100% line coverage. mago format --check, mago lint and mago analyze are clean; one lint-baseline entry for Statement.php (kan-defect) no longer applies and has been dropped.

bindParametersFromContainer() returned early once parameters had been
bound, so re-executing a prepared statement with new values (via an
array, a replacement ParameterContainer, or a mutated container) ran
again with the first values. Bind from the current container on every
execute instead.
@codecov

codecov Bot commented Oct 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@simon-mundy
simon-mundy requested a review from tyrsson October 12, 2026 02:50
@simon-mundy simon-mundy self-assigned this Oct 12, 2026
@simon-mundy simon-mundy added the bug Something isn't working label Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

1 participant