简体   繁体   English

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

[英]Converting a Mysql Result Object to Associative Array (CodeIgniter)

I'm trying to get a database query which is an object converted to an associative array, so that I can use it in the calendar class in codeigniter. 我正在尝试获取数据库查询,这是一个转换为关联数组的对象,以便我可以在codeigniter中的日历类中使用它。

This is my model: 这是我的模特:

<?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;
        }
    }

and this is the error I get: 这是我得到的错误:

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

Any ideas? 有任何想法吗?

Check ou this video tutorial, it will help you -> http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-the-calendar-library/ 看看这个视频教程,它会对你有所帮助 - > http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-the-calendar-library/

Your model should look like this: 您的模型应如下所示:

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

and your controller: 和你的控制器:

    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);
}

The problem was not in your use of result_array(), more that you later return $data directly. 问题不在于你使用result_array(),而是你以后直接返回$ data。 $query = $this->db->query() then use $query->result_array() in the foreach. $ query = $ this-> db-> query()然后在foreach中使用$ query-> result_array()。 Then you can return $data after building it up in the foreach. 然后,您可以在foreach中构建后返回$ data。

The other answer is a long-winded way of writing the following: 另一个答案是编写以下内容的冗长方式:

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

But of course that will return an array of objects, not an multidimensional array. 但是当然会返回一个对象数组,而不是多维数组。

Use below simple method, 使用以下简单方法,

$query = $this->db->get_where('table', array('table_id' => $id));

        $queryArr = $query->result();        

        foreach ($queryArr[0] as $key=>$val)
        {
            $row[$key]=$val;
        }

print_r($row); //this will give associative array

Here is the solution for CodeIgniter-3 这是CodeIgniter-3的解决方案

function getAllDiaries(){
    $query = $this->db->query("YOUR QUERY HERE");
    return $query->result('array');
}

OR 要么

function getAllDiaries(){
    return $this->db->query("YOUR QUERY HERE")->result('array');
}

Note: result() function accept "array" or "object" as parameter. 注意: result()函数接受“array”或“object”作为参数。 Default is "object" 默认为“对象”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM