PDO::query(): FETCH_CLASS constructor arguments error reports the wrong argument numbers

Open Beginner friendly
#25,650 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c
Domain
database

Research direction

The wrong numbers come from the constructor check in pdo_stmt_setup_fetch_mode() in ext/pdo/pdo_stmt.c, which hardcodes 3 and #2 where the function already computes constructor_arg_num and arg1_arg_num. Compare that check with the other errors in the same function, then read pdo_stmt_class_ctor_errors.phpt to see how existing cases are tested. Done means the PDO::query() case reports argument #4 and #3, and setFetchMode() output is unchanged.

Written by the indexing model from the issue text.

Description

Description

PDO::query() reports the wrong argument numbers when PDO::FETCH_CLASS gets constructor arguments for a class without a constructor.

The following code:

<?php
class NoCtor {}

$db = new PDO('sqlite::memory:');

try {
    $db->query('SELECT 1', PDO::FETCH_CLASS, 'NoCtor', [1]);
} catch (Throwable $e) {
    echo $e::class, ': ', $e->getMessage(), "\n";
}

try {
    $db->query('SELECT 1', PDO::FETCH_CLASS, 'NotAClass');
} catch (Throwable $e) {
    echo $e::class, ': ', $e->getMessage(), "\n";
}

Resulted in this output:

ValueError: PDO::query(): Argument #3 must be empty when class provided in argument #2 ($class) does not have a constructor
TypeError: PDO::query(): Argument #3 must be a valid class

But I expected this output instead:

ValueError: PDO::query(): Argument #4 must be empty when class provided in argument #3 ($class) does not have a constructor
TypeError: PDO::query(): Argument #3 must be a valid class

In PDO::query() the class is argument #3 (the second error says so) and the constructor arguments are argument #4.

PDO::query() and PDOStatement::setFetchMode() share pdo_stmt_setup_fetch_mode() in ext/pdo/pdo_stmt.c. The callers pass the position of the fetch mode (1 from setFetchMode(), 2 from PDO::query()), and the function computes arg1_arg_num and constructor_arg_num from it. All errors in the function use these values, except the constructor check, which hardcodes 3 and #2. That is only right for setFetchMode().

The message was added in 3ff7758bcf7 (GH-17427), so PHP 8.5 and later are affected. PHP 8.4 is not affected: it reports a different error for this case. pdo_stmt_class_ctor_errors.phpt covers fetchAll() and setFetchMode(), but not PDO::query().

A fix is to use constructor_arg_num and arg1_arg_num in this message. I have a patch with a test and will open a PR against PHP-8.5.

The quoted paragraphs were drafted with an LLM. The code and the output above come from real runs on PHP 8.4, 8.5 and 8.6.

PHP Version
PHP 8.5.12 (cli) (built: Oct 28 2026 01:20:43) (NTS)
Copyright (c) The PHP Group
Built by https://github.com/docker-library/php
Zend Engine v4.5.12, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.12, Copyright (c), by Zend Technologies

Also reproduced on PHP 8.6.0 and on the PHP-8.5 branch (8.5.14-dev).

Operating System

Debian 13 (php:8.5-cli Docker image)

Dominant language
C
Stars
41.8k
Forks
8.6k
Avg merge
2d 15h
Merged PRs (30d)
125

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from php/php-src

All issues in php/php-src

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.