Issues converting stdClass to array

Tags: , , ,



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.

Answer

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


Source: stackoverflow