0

似乎在这里经常出现此错误,查看答案我仍然无法弄清楚为什么会出现错误。

我收到错误

致命错误:在第83行调用非对象上的成员函数 result()

有问题的行与控制器中的此功能有关- 这就是造成错误的原因。

$this->view_data['categories'] = $my_categories->result();

功能如下。。

function _load_search_options()
{
    // Get all the categories for the advanced search page
    $my_categories = $this->Categories_model->get_all();
    $this->view_data['categories'] = $my_categories->result();

    // Get all the PRIMARY colours from teh tbl_colour_options
    $my_colour_options = $this->Colours_model->get_all_primary();
    $this->view_data['colour_options'] = $my_colour_options->result();


    // Get all the colours from teh tbl_colour_options
    $my_colour_options_all = $this->Colours_model->get_all();
    $this->view_data['colour_options_all'] = $my_colour_options_all->result();
}

我的模型如下...

function get_all()
{
    $query_str = "
        SELECT *
        FROM categories
        WHERE CATEGORIES_parent_id = 0
    ";

    $results = $this->db->query($query_str);
    $parents = $results->result();

    foreach ($parents as $parent)
    {
        $children = array();

        $query_str = "
            SELECT *
            FROM categories
            WHERE CATEGORIES_parent_id = '$parent->CATEGORIES_id'
        ";

        $children_results = $this->db->query($query_str);
        $children_results = $children_results->result();

        foreach($children_results as $children_result)
        {
            $children[$children_result->CATEGORIES_id] = $children_result->CATEGORIES_title;
        }

        $categories[$parent->CATEGORIES_title] = $children;




    }

    return $categories;
}

值得注意的是,在 MySQL 中单独运行 SELECT 查询会带来一些结果。

4

1 回答 1

1

该函数get_all不返回数据库对象(资源)。

函数中的$categories变量get_all被定义为一个数组。

您不能将其作为对象访问或调用该results函数。

于 2012-06-06T14:53:54.893 回答