Bug #75387 [Opn->Csd]: fetchAll complains about extraneous parameters

From: Date: Fri, 03 May 2019 08:40:29 +0000
Subject: Bug #75387 [Opn->Csd]: fetchAll complains about extraneous parameters
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-220682@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75387&edit=1

 ID:                 75387
 User updated by:    aljosha dot papsch at vinexus dot eu
 Reported by:        aljosha dot papsch at vinexus dot eu
 Summary:            fetchAll complains about extraneous parameters
-Status:             Open
+Status:             Closed
 Type:               Bug
 Package:            PDO related
 PHP Version:        7.1.10
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2019-05-02 21:13:19] 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);
    }

------------------------------------------------------------------------
[2018-03-02 10:27:38] 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.

------------------------------------------------------------------------
[2017-10-18 07:38:57] 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-16 10:25:04] 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.



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=75387&edit=1


Thread (5 messages)

« previous php.bugs (#220682) next »