php – 将Mysql结果对象转换为关联数组(CodeIgniter)

前端之家收集整理的这篇文章主要介绍了php – 将Mysql结果对象转换为关联数组(CodeIgniter)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试获取数据库查询,这是一个转换为关联数组的对象,以便我可以在codeigniter中的日历类中使用它.

这是我的模特:

<?PHP

class Get_diary_model extends Model {

    function getAllDiaries($year,$month) {

        $data = $this->db->query("SELECT day AND entry FROM diary WHERE month=$month AND year=$year"); // the entries for the relevant month and year

        foreach($data->result_array() as $row) { // return result as assoc array to use in calendar
            echo $row['day'];
            echo $row['entry'];
        }

        return $data;
        }
    }

这是我得到的错误

atal error: Cannot use object of type CI_DB_MysqL_result as array in C:\wamp\www\mm\system\libraries\Calendar.PHP on line 219

有任何想法吗?

看看这个视频教程,它会对你有所帮助 – > http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-the-calendar-library/

您的模型应如下所示:

function getAllDiaries($year,$month)
    {
        $q = $this->db->query("SELECT day AND entry FROM diary WHERE month=$month AND year=$year");

        if($q->num_rows() > 0):
            foreach($q->result() as $row):
                $data[] = $row;
            endforeach;
            return $data;
        else:
            return false;
        endif;
    }

和你的控制器:

function index($year = null,$month = null)
    {
        $this->load->model('Get_diary_model');

        if (!$year) {
            $year = date('Y');
        }
        if (!$month) {
            $month = date('m');
        }

        $data['calendar'] = $this->Get_diary_model->getAllDiaries($year,$month);
}
原文链接:https://www.f2er.com/php/137609.html

猜你在找的PHP相关文章