php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75387 fetchAll complains about extraneous parameters
Submitted: 2017-10-16 10:25 UTC Modified: 2019-05-03 08:40 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: aljosha dot papsch at vinexus dot eu Assigned:
Status: Closed Package: PDO related
PHP Version: 7.1.10 OS:
Private report: No CVE-ID: None
 [2017-10-16 10:25 UTC] aljosha dot papsch at vinexus dot eu
Description:
------------
I created a small wrapper around PDO. My first stab at wrapping fetchAll looked like this:

    public function fetchAll($fetchStyle = \PDO::FETCH_BOTH,
                             $fetchArgument = null,
                             array $constructorArgs) {
        return $this->st->fetchAll($fetchStyle, $fetchArgument, $constructorArgs);
    }


PDO rejected the method call, complaining:

    SQLSTATE[HY000]: General error: Extraneous additional parameters

So I had to turn the simple one liner into:

    public function fetchAll($fetchStyle = \PDO::FETCH_BOTH,
                             $fetchArgument = null,
                             array $constructorArgs = []) {
        if ($fetchStyle === \PDO::FETCH_BOTH) {
            return $this->st->fetchAll();
        }
        elseif ($fetchStyle === \PDO::FETCH_CLASS) {
            return $this->st->fetchAll($fetchStyle, $fetchArgument, $constructorArgs);
        }
        elseif ($fetchStyle === \PDO::FETCH_ASSOC ||
                $fetchStyle === \PDO::FETCH_NUM   ||
                $fetchStyle === \PDO::FETCH_ASSOC) {
            return $this->st->fetchAll($fetchStyle);
        }
        else {
            return $this->st->fetchAll($fetchStyle, $fetchArgument);
        }
    }

It would be great if PDO would not complain about extraneous arguments and just ignore arguments not needed. Otherwise, any PDO wrapper has to do this logic again of its own, possibly with errors. For example, there is a bug in the code above: I forgot considering \PDO::FETCH_OBJ in the last elseif block.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-10-18 07:38 UTC] yannis dot berrouag at gmail dot com
I don't think it's a good idea, you are trying to use a function incorrectly and php warns you about that, this is a perfectly normal behavior.

if you are writing a wrapper, why don't you just write yourself simpler methods like fetchAssoc(), fetchBoth() etc ... and make the correct call to fetchAll inside.
 [2017-10-24 08:32 UTC] kalle@php.net
-Package: PDO Core +Package: PDO related
 [2018-03-02 10:27 UTC] aljosha dot papsch at vinexus dot eu
Originally, I just wanted a thin wrapper ironing out some usage issues I perceived with PDO. But you are right, it's a good opportunity to create a more convenient interface. No need to keep this bug open.
 [2019-05-02 21:13 UTC] keithy at consultant dot com
Simple subclassing of PDOStatement and overriding of fetchAll(...) is broken.

Necessitating code like such:
   

 function fetchAll($how = null, $class_name = null, $ctor_args = null) {
        if (!isset($ctor_args)) {
            if (!isset($class_name)) {
                if (!isset($how)) {
                    return $this->PDOStatement->fetchAll();
                }
                return $this->PDOStatement->fetchAll($how);
            }
            return $this->PDOStatement->fetchAll($how, $class_name);
        }
        return $this->PDOStatement->fetchAll($how, $class_name, $ctor_args);
    }
 [2019-05-03 08:40 UTC] aljosha dot papsch at vinexus dot eu
-Status: Open +Status: Closed
 [2019-05-03 08:40 UTC] aljosha dot papsch at vinexus dot eu
Keithy, good point. The funny workarounds are necessary, because the documentation at https://www.php.net/manual/en/pdostatement.fetchall.php seems to suggest that all arguments are optional and may lead you to think that you could pass the default values (null or []). In reality however, what you may pass depends on the first argument.

So the advice from Yannis is still valid:

> if you are writing a wrapper, why don't you just write yourself simpler methods like fetchAssoc(), fetchBoth() etc ... and make the correct call to fetchAll inside.

It's true, don't limit yourself to the PDO interface. You will sleep better.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 13:01:28 2024 UTC