Codeigniter/PHP:将数据库查询格式化为数组 [英] Codeigniter/PHP: Format db query as an array

查看:43
本文介绍了Codeigniter/PHP:将数据库查询格式化为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    $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...

即.我想做类似 $ data ['user_individual'] ['id']-> format_as_array ...

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

推荐答案

CodeIgniter提供了几种对查询结果执行的方法.

CodeIgniter provides several methods to perform on query results.

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

result()返回一个PHP对象数组.

result() returns an array of PHP objects.

row()返回该行的单个PHP对象.

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

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

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

row_array() returns a single array for that row.

row() row_array()可以选择使用一个参数,该参数是您要返回的行号.

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

除此之外,很难准确说明您的要求.使用这些方法,您应该能够完全按照自己的意愿提取数据.

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.

修改:

偶然地,访问这些方法的方法是通过从 $ this-> db-> get()调用返回的查询对象:

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

$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

这篇关于Codeigniter/PHP:将数据库查询格式化为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆