Skip to content

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

Done

Description

@thunderer

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 3ff7758 (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)

Activity

  1. added a commit that references this issue on Nov 22, 2026
    961d6c7
  2. added a commit that references this issue on Nov 24, 2026
    4021e1d
  3. added a commit that references this issue on Nov 25, 2026
    ec39abf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions