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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3725980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:53:38  来源:igfitidea点击:

Converting a Mysql Result Object to Associative Array (CodeIgniter)

phpmysqlcodeigniter

提问by Robimp

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?

有任何想法吗?

采纳答案by Christophe

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

回答by Phil Sturgeon

The problem was not in your use of result_array(), more that you later return $data directly. $query = $this->db->query() then use $query->result_array() in the foreach. Then you can return $data after building it up in the foreach.

问题不在于您使用 result_array(),而是您后来直接返回 $data。$query = $this->db->query() 然后在 foreach 中使用 $query->result_array() 。然后你可以在 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.

但是当然这将返回一个对象数组,而不是一个多维数组。

回答by Kiran

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

回答by Sumon Sarker

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. Default is "object"

注意:result() 函数接受“数组”或“对象”作为参数。默认为“对象”