1

I have converted one of my websites to use PDO database queries. All the queries are working perfectly except one and I have spent all day trying to solve this problem and nothing is working for me so as a last resort I am asking for some advice here.

This is the function I am having the problem with

    public function getTransactions($iProfileId)
    {

        $rStmt = Db::getInstance()->prepare('SELECT * FROM' . Db::prefix(DbTableName::GIFT_TX) .
                                            'WHERE profileId = :profileId ' .
                                            'ORDER BY createDate ASC, txId ASC');
        
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);

        $rStmt->execute();
        $this->getTxs = $rStmt->fetch(\PDO::FETCH_OBJ);
        Db::free($rStmt);
        
                    
        return $this->getTxs;
    }
    

It returns a single row even though there are many others and as an object


object(stdClass)#47 (8) { ["txId"]=> string(1) "8" ["profileId"]=> string(3) "861" ["type"]=> string(6) "credit" ["credits"]=> string(2) "25" ["createDate"]=> string(19) "2020-06-26 10:48:55" ["isDelete"]=> string(1) "0" ["price"]=> string(4) "0.05" ["merchant"]=> string(6) "PayPal" }


I need this to be returned as an array and to get all the rows with profileId = :profileId

I have tried everything I can find online and have had no success at all.

8
  • You only call fetch() once and you call it with \PDO::FETCH_OBJ, so you will end up with 1 row as an object! Commented Jun 26, 2020 at 19:05
  • PHP docs say... PDO::FETCH_OBJ (integer) Specifies that the fetch method shall return each row as an object with property names that correspond to the column names returned in the result set. Commented Jun 26, 2020 at 19:08
  • I have over 200 functions and this is the only one I have an issue with. Commented Jun 26, 2020 at 19:09
  • 1
    On the manual page of fetch() PDO::FETCH_OBJ: returns an anonymous object with property names that correspond to the column names returned in your result set Commented Jun 26, 2020 at 19:11
  • Try fetchAll() with \PDO::FETCH_ASSOC. Commented Jun 26, 2020 at 19:12

1 Answer 1

1

Try this code

$this->getTxs = $rStmt->fetchAll(PDO::FETCH_ASSOC);
    

If in doubt, check out the URL below:

https://www.php.net/manual/en/pdostatement.fetchall.php
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.