Codeigniter错误:致命错误:不能将CI_DB_mysql_result类型的对象用作数组 [英] Codeigniter error : Fatal error: Cannot use object of type CI_DB_mysql_result as array

查看:53
本文介绍了Codeigniter错误:致命错误:不能将CI_DB_mysql_result类型的对象用作数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为树实现php codeigniter模型,并且每次运行控制器时,都会出现以下错误:

I'm trying to implement a php codeigniter model for tree and every time I run my controller, I am getting the following error :

致命错误:无法将CI_DB_mysql_result类型的对象用作数组第50行的C:\ AppServ \ www \ tree \ application \ models \ Btree.php

Fatal error: Cannot use object of type CI_DB_mysql_result as array in C:\AppServ\www\tree\application\models\Btree.php on line 50

我试图在行中修复此语法

I tried to fix this syntax in line

$currentID = $row['id'];

但是,仍然收到相同的错误消息.

however, still getting the same error message.

我的模型函数是:

public function fetchTree($parentArray, $parentID = null)
{
    // Create the query
    if ($parentID == null)
        $parentID = -1;

    $sql = "SELECT `id` FROM `{$this->tblName}` WHERE `id`= ". intval($parentID);

    // Execute the query and go through the results.
    $result = $this->db->query($sql);
    if ($result)
    {
        while ($row = $result)
        {
            // Create a child array for the current ID
            $currentID = $row['id'];
            $parentArray[$currentID] = array();

            // Print all children of the current ID
            $this->fetchTree($parentArray[$currentID], $currentID);
        }
        $result->close();
    }
}

推荐答案

您的问题在于以下行:

while($row = $result)

您要将 $ row 设置为整个查询对象.您想遍历查询的结果.

You're setting $row to the entire query object. You want to loop through the results of the query.

尝试以下方法:

$query = $this->db->query($sql);
foreach($query->result_array() AS $row) {
    $currentID = $row['id'];
    ...

这篇关于Codeigniter错误:致命错误:不能将CI_DB_mysql_result类型的对象用作数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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