Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force PDOStatement->fetchAll to return array of objects?

Tags:

object

php

pdo

I am writing my own simply ORM using PDO. My question is if you can force PDOStatement::fetchAll() method to return array of objects of stdClass? For example:

$result = $q->fetch_all(/* some magic here */);
print_r($result);

Should print something like:

Array
(
    [0] => stdClass Object
        (
            [NAME] => pear
            [COLOUR] => green
        )

    [1] => stdClass Object
        (
            [NAME] => watermelon
            [COLOUR] => pink
        )

)

Is this posible? NAME and COLOUR are of course names of columns. I read documentation but I didn't find anything interesting.

like image 518
Elektryk Avatar asked Sep 09 '25 16:09

Elektryk


1 Answers

Use $result = $q->fetchAll(PDO::FETCH_OBJ);


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!