Codeigniter/PHP:将数据库查询格式化为数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4610276/
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 13:41:04  来源:igfitidea点击:

Codeigniter/PHP: Format db query as an array

phpcodeigniter

提问by Kevin Brown

    $this->db->select('id, user_id')->from('be_users')->where('id',$user_id);
    $data['user_individual'] = $this->db->get();

If this is my db query, how do I get an array of one db row...

如果这是我的数据库查询,我如何获得一个数据库行的数组...

ie. I want to do something like $data['user_individual']['id']->format_as_array...

IE。我想做一些像$data['user_individual']['id']->format_as_array......

回答by treeface

CodeIgniter provides several methods to perform on query results.

CodeIgniter 提供了几种方法来执行查询结果。

See here: https://codeigniter.com/user_guide/database/results.html

请参阅此处:https: //codeigniter.com/user_guide/database/results.html

result()returns an array of PHP objects.

result()返回一组 PHP 对象。

row()returns a single PHP object for that row.

row()为该行返回单个 PHP 对象。

result_array()returns an array of arrays.

result_array()返回一个数组数组。

row_array()returns a single array for that row.

row_array()返回该行的单个数组。

row()and row_array()optionally take a parameter which is the number of the row you'd like to return.

row()并可row_array()选择采用一个参数,该参数是您要返回的行数。

Beyond this, it's difficult to tell exactly what you're asking for. You should be able to get your data out exactly as you like with these methods.

除此之外,很难确切地说出您的要求。使用这些方法,您应该能够完全按照自己的意愿获取数据。

Edit:

编辑

Incidentally, the way to access these methods is through the query object returned from the $this->db->get()call:

顺便说一下,访问这些方法的方式是通过$this->db->get()调用返回的查询对象:

$query = $this->db->get();

$rows = $query->result(); //array of objects
$rows_array = $query->result_array(); //array of arrays
$row = $query->row(); //single object
$row_array = $query->row_array(); //single array

回答by Highway of Life

When you use $this->db->get();you can use $this->db->result();which returns an arrayobject.

当您使用时,$this->db->get();您可以使用$this->db->result();which 返回一个数组对象。

$query = $this->db->get('table_name');

foreach ($query->result() as $row)
{
    echo $row->title;
}

See: http://codeigniter.com/user_guide/database/results.htmlfor details on how to obtain resultsets from the database.

有关如何从数据库获取结果集的详细信息,请参阅:http: //codeigniter.com/user_guide/database/results.html。

回答by Жасулан Бердибеков

Instead of $ this-> db-> get () use $ this-> db-> row_array ();

而不是 $this->db->get() 使用 $this->db->row_array();

$this->db->select('id, user_id')->from('be_users')->where('id',$user_id);
$data['user_individual'] = $this->db->row_array();

回答by Gaurang panchani

/** * format_database_array * * Lets you format the database object data to array

/** * format_database_array * * 让您将数据库对象数据格式化为数组

  • @access public
  • @param String $db_object : database query object
  • @return String : Return array with data */
  • @访问公开
  • @param String $db_object : 数据库查询对象
  • @return String : 返回包含数据的数组 */

if ( ! function_exists('format_database_array')) {

如果(!function_exists('format_database_array')){

    function format_database_array($db_object)
{
    $db_array = array();

    if($db_object-> num_rows >0)
    {
        foreach ($db_object->result_array() as $row)
        {
            foreach ($row as $key => $value)
            {
                $db_array[$key] = $value;
            }
        }
    }

    return $db_array;
}

}

}