Skip to content

Fix GH-20214: PDO::FETCH_DEFAULT unexpected behavior with setFetchMode#21434

Open
iliaal wants to merge 1 commit intophp:masterfrom
iliaal:fix/gh-20214-pdo-fetch-default
Open

Fix GH-20214: PDO::FETCH_DEFAULT unexpected behavior with setFetchMode#21434
iliaal wants to merge 1 commit intophp:masterfrom
iliaal:fix/gh-20214-pdo-fetch-default

Conversation

@iliaal
Copy link
Contributor

@iliaal iliaal commented Mar 13, 2026

When setFetchMode(PDO::FETCH_DEFAULT) is called, pdo_stmt_setup_fetch_mode() stores PDO_FETCH_USE_DEFAULT (0) as the statement's default fetch type. Later, do_fetch() tries to resolve PDO_FETCH_USE_DEFAULT by reading stmt->default_fetch_type — which is also 0, creating a circular reference. On 8.4 this silently fell through to FETCH_BOTH; on master it throws a ValueError.

The fix resolves PDO_FETCH_USE_DEFAULT to the connection-level default (stmt->dbh->default_fetch_type) early in pdo_stmt_setup_fetch_mode(), before flags extraction and the mode switch. The connection default is guaranteed to never be PDO_FETCH_USE_DEFAULT (enforced by PDO::setAttribute), so no circular resolution is possible.

Not sure if this should be rebased to the PHP-8.4 branch since the bug affects 8.4 as well (silently returns wrong fetch mode there).

Fixes #20214

…ment::setFetchMode

When setFetchMode(PDO::FETCH_DEFAULT) is called, mode=0
(PDO_FETCH_USE_DEFAULT) gets stored as the statement's default fetch
type. Later, do_fetch() tries to resolve PDO_FETCH_USE_DEFAULT by
reading stmt->default_fetch_type, which is also 0 — circular
reference that on 8.4 silently fell through to FETCH_BOTH and on
master throws a ValueError.

Resolve PDO_FETCH_USE_DEFAULT to the connection-level default early in
pdo_stmt_setup_fetch_mode(), before flags extraction and the mode
switch, so the rest of the function processes the actual fetch mode.

Closes phpGH-20214
@iliaal iliaal force-pushed the fix/gh-20214-pdo-fetch-default branch from 40d5d5a to 72d1b29 Compare March 14, 2026 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PDO::FETCH_DEFAULT unexpected behavior with PDOStatement::setFetchMode

1 participant