使用codeigniter对查询做一个foreach?

时间:2012-08-10 14:30:44

标签: php mysql codeigniter model

我正在尝试使用 codeigniter 对我的模型进行foreach,但它会给我一个错误并返回结果。

以下是代码:

public function read(){
        $questions; //array
        $query = $this->db->get('questions');
        foreach($query->result() as $row){
            $getAnswers = $this->db->get_where('answers', array('question_id'=>$row['question_id]']), 4);
            $questions= $getAnswers;

           }

        return $questions;
     }

这是错误:

  

致命错误:无法在

中使用stdClass类型的对象作为数组

1 个答案:

答案 0 :(得分:4)

访问row作为对象:

$getAnswers = $this->db->get_where('answers', 
                   array('question_id'=>$row->question_id), 4);