Bug #54545 [Opn->Wfx]: PDO::query throws exceptions with null optional arguments
| From: | nikic@php.net | Date: | Wed, 28 Oct 2020 15:15:15 +0000 |
| Subject: | Bug #54545 [Opn->Wfx]: PDO::query throws exceptions with null optional arguments | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-229986@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=54545&edit=1
ID: 54545
Updated by: nikic@php.net
Reported by: php at bucksvsbytes dot com
Summary: PDO::query throws exceptions with null optional
arguments
-Status: Open
+Status: Wont fix
Type: Bug
Package: PDO MySQL
Operating System: ubuntu
PHP Version: 5.3.6
Block user comment: N
Private report: N
New Comment:
Closing this as Won't Fix. In PHP 8, the signature for PDO::query() is:
public function query(string $query, ?int $fetchMode = null, mixed ...$fetchModeArgs) {}
which now gives explicit indication that the method accepts a variable number of arguments.
If you need to call query() generically, using different fetch modes, then you can also use argument
unpacking to do so:
public function myQuery(string $query, ?int $fetchMode = null, mixed ...$fetchModeArgs) {
return $this->pdo->query($query, $fetchMode, ...$fetchModeArgs);
}
Previous Comments:
------------------------------------------------------------------------
[2020-08-29 14:36:46] cmb@php.net
> The documentation of PDO::query doesn't show any initializers, [â¦]
That has changed in the meantime for $fetch_style.
> 'SQLSTATE[HY000]: General error: mode must be an integer'
In PHP 5 & 7, PDO doesn't type juggle the $fetch_style[1]. That
changes as of PHP 8.0.0. The other parameters are strictly typed
in all versions. That should be documented.
[1] <https://github.com/php/php-src/blob/php-7.4.9/ext/pdo/pdo_dbh.c#L1091>
------------------------------------------------------------------------
[2011-04-17 10:31:00] php at bucksvsbytes dot com
Your point about initializers is absolutely correct, of course, but it seems unnecessarily
cumbersome (and atypical in PHP) to require this awkward construction:
if ($a) then{
$x=function($p1)
}elseif($b) then{
$x=function($p1,$p2)
}elseif($c) then{
$x=function($p1,$p2,$p3)
}
rather than simply
$x=function($p1,$p2,$p3)
with the value of p2 and p3 determining which function logic to execute.
I leave it in your hands. Thanks for your responses.
------------------------------------------------------------------------
[2011-04-17 03:14:39] cataphract@php.net
> The only problem is that a null argument should be equivalent to an omitted
> argument for error checking purposes
I don't know where you got this idea. An omitted argument should be equivalent to whatever
initializer is specified. The documentation of PDO::query doesn't show any initializers, so you
can't assume you can pass NULLs. The fact this rule is only conventional in internal functions
(there's no enforced initializer functionality) doesn't make it less true; if an
initializer in the docs don't match the behavior, it's a bug.
That said, it might be appropriate to have nulls have the same effect as missing arguments.
------------------------------------------------------------------------
[2011-04-16 20:57:36] php at bucksvsbytes dot com
It doesn't appear to be a package issue to me. The error is caused by specific checking of the
argument values, proved by the different exceptions below. The only problem is that a null argument
should be equivalent to an omitted argument for error checking purposes. These exception appeared
after switching from a 5.2 PHP server to a 5.3.
Example 1 (arg2, arg3, and arg4 of query are null):
exception 'PDOException' with message 'SQLSTATE[HY000]: General error: mode must be
an integer' in /home/bvb/www/html/cl/bvc_d.php:681
Stack trace:
#0 /home/bvb/www/html/cl/bvc_d.php(681): PDO->query('select * from p...', NULL, NULL,
NULL)
Example 2 (arg2 of query is 0, arg3 and arg4 are null):
exception 'PDOException' with message 'SQLSTATE[HY000]: General error: fetch mode
doesn't allow any extra arguments' in /home/bvb/www/html/cl/bvc_d.php:681
Stack trace:
#0 /home/bvb/www/html/cl/bvc_d.php(681): PDO->query('select * from p...', 0, NULL,
NULL)
My PDO driver is pgsql. The Configure Command display of phpinfo() is missing in 5.3, so please
advise what equivalent data I should supply for the PHP configure line.
------------------------------------------------------------------------
[2011-04-16 19:47:05] johannes@php.net
Which driver are you using? What's the PHP configure line? And well ... verification on 5.3.6
or svn snapshot would be great ... Please also mind that if you're using distribution packages
you might issues due to their patches, which we can't verify.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=54545
--
Edit this bug report at https://bugs.php.net/bug.php?id=54545&edit=1