PDO::query(): FETCH_CLASS constructor arguments error reports the wrong argument numbers
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
錯誤的數字來自 ext/pdo/pdo_stmt.c 中 pdo_stmt_setup_fetch_mode() 的建構函式檢查,它硬編碼了 3 和 #2,但該函式其實已經計算了 constructor_arg_num 和 arg1_arg_num。請將這項檢查與同一函式中的其他錯誤比較,然後閱讀 pdo_stmt_class_ctor_errors.phpt,了解既有案例是如何測試的。當 PDO::query() 案例回報參數 #4 和 #3,且 setFetchMode() 的輸出保持不變時,即完成。
由索引模型根據 Issue 內容生成。
描述
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)
- 主要語言
- C
- 星號
- 41.8k
- 分支
- 8.6k
- 平均合併
- 2 天 15 小時
- 30 天內合併 PR
- 125
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
php/php-src 的其他 Issue
-
ext/openssl: 8.2.31+/8.3.31+ fail to build against OpenSSL 1.0.2 (ASN1_STRING_get0_data in xp_ssl.c)未關閉Extension: openssl Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 79/100
維護者通常 1 天內回覆
-
Bug Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 68/100
維護者通常 1 天內回覆
-
SNMP accepts malformed remote ports可能已有人在做 @somethingwithproof 於 21 天前認領。 未關閉
難度 2/5 半天 新手友好度 62/100
維護者通常 1 天內回覆
-
Bug Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 64/100
維護者通常 1 天內回覆
-
Bug Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 62/100
維護者通常 1 天內回覆
相似的 Issue
-
bug
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 82/100
python-pillow/Pillow#10590 ·
維護者通常 1 天內回覆
-
bug
難度 2/5 1-3 小時 新手友好度 62/100
openwrt/packages#31407 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 62/100
CCExtractor/ccextractor#2478 ·
維護者通常 7 天內回覆
-
難度 2/5 1-3 小時 新手友好度 66/100
維護者通常 1 天內回覆