简体   繁体   English

CodeIgniter中是否有一个函数将数据库查询结果的所有行作为单个数组获取?

[英]Is there a function in CodeIgniter to get all rows of the database query result as single array?

Is there a function in CodeIgniter to get all rows of the database query result as single array, rather than a resource that has to be iterated. CodeIgniter中是否有一个函数将数据库查询结果的所有行作为单个数组获取,而不是必须迭代的资源。 Sometimes all rows in one array is all one needs. 有时,一个阵列中的所有行都是需要的。

Something like: 就像是:

$this->db->query("MULTI-ROW QUERY")->all_rows();

Yes, there is. 就在这里。

$new_array = $this->db->query("MULTI-ROW QUERY")->result_array();
print_r($new_array);

Go through the following link for detail: Generating Query Results 有关详细信息,请通过以下链接: 生成查询结果

This function returns the query result as a pure array, or an empty array when no result is produced. 此函数将查询结果作为纯数组返回,或者在未生成结果时返回空数组。 Typically you'll use this in a foreach loop, like this: 通常你会在foreach循环中使用它,如下所示:

$query = $this->db->query("YOUR QUERY");

foreach ($query->result_array() as $row)
{
   echo $row['title'];
   echo $row['name'];
   echo $row['body'];
}

$this->db->get(TABLE)将返回TABLE中的所有行

Just try: 你试一试:

//here the $rows will be an array
$rows = $query->result_array ();

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

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