php - 使用 codeigniter 对查询执行 foreach?

标签 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;
     }

这是错误:

Fatal error: Cannot use object of type stdClass as array in

最佳答案

row 作为对象访问:

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

关于php - 使用 codeigniter 对查询执行 foreach?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11903811/

相关文章:

php - 错误: wrong mysql library version or lib not found (when building PHP 7 with a custom built MySQL 8)

mysql - 如何在MySql中转义撇号(单引号)?

mysql - 当我使用 select 语句时,我的 sql 多次输出相同的数据

mysql - 使用本地用户作为 MySQL 用户

php - 对于 jquery 脚本中的每个 php 变量

php - fatal error : Call to undefined function form_open() in c

codeigniter - 重复的元描述和标题标签

javascript - 如何从PHP获取特定的radio值

php - 文件写入在 PHP 扩展 c/c++ (linux) 中不起作用

php - 在我的用户类中使用数据库类