PDO::query(): FETCH_CLASS constructor arguments error reports the wrong argument numbers
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
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 whenPDO::FETCH_CLASSgets 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()andPDOStatement::setFetchMode()sharepdo_stmt_setup_fetch_mode()inext/pdo/pdo_stmt.c. The callers pass the position of the fetch mode (1fromsetFetchMode(),2fromPDO::query()), and the function computesarg1_arg_numandconstructor_arg_numfrom it. All errors in the function use these values, except the constructor check, which hardcodes3and#2. That is only right forsetFetchMode().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.phptcoversfetchAll()andsetFetchMode(), but notPDO::query().A fix is to use
constructor_arg_numandarg1_arg_numin 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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from php/php-src
-
ext/openssl: 8.2.31+/8.3.31+ fail to build against OpenSSL 1.0.2 (ASN1_STRING_get0_data in xp_ssl.c)OpenExtension: openssl Status: Needs Triage
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
php/php-src#25402 · 2 comments ·
Maintainers usually reply within 1 day
-
Bug Status: Needs Triage
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
php/php-src#25397 · 3 comments ·
Maintainers usually reply within 1 day
-
SNMP accepts malformed remote portsPossibly taken @somethingwithproof claimed this 21 days ago. Open
Difficulty 2/5 Half a day Newbie friendliness 62/100
php/php-src#25137 · 1 comment ·
Maintainers usually reply within 1 day
-
Bug Status: Needs Triage
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
Maintainers usually reply within 1 day
-
Bug Status: Needs Triage
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
php/php-src#24196 · 1 comment ·
Maintainers usually reply within 1 day
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
python-pillow/Pillow#10590 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
openwrt/packages#31407 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
CCExtractor/ccextractor#2478 ·
Maintainers usually reply within 7 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
Maintainers usually reply within 1 day