如何强制PDOStatement-> fetchAll返回对象数组? [英] How to force PDOStatement->fetchAll to return array of objects?

查看:109
本文介绍了如何强制PDOStatement-> fetchAll返回对象数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PDO编写自己的简单ORM.我的问题是您是否可以强制PDOStatement::fetchAll()方法返回stdClass对象的数组?例如:

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);

应打印如下内容:

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

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

)

这是可能的吗? NAME和COLOR当然是列的名称.我阅读了文档,但没有发现任何有趣的东西.

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

推荐答案

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

这篇关于如何强制PDOStatement-> fetchAll返回对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆