jayarjo
jayarjo

Reputation: 16726

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. Sometimes all rows in one array is all one needs.

Something like:

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

Upvotes: 2

Views: 4018

Answers (3)

Casperon
Casperon

Reputation: 107

$this->db->get(TABLE) will return all rows from TABLE

Upvotes: 2

hsuk
hsuk

Reputation: 6860

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:

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

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

Upvotes: 5

Andrew
Andrew

Reputation: 5340

Just try:

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

Upvotes: 1

Related Questions