Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between row_array and result_array

What is the difference between row_array() and result_array()?

How would they be displayed on a view page?

if ($variable) {
    return $result->row_array();
} else {
    return $result->result_array();
}
like image 516
Shay Avatar asked Apr 22 '14 22:04

Shay


2 Answers

From the documentation, row_array returns a single result and result_array returns multiple results (usually for use in a loop).

Examples from the documentation:

Result_array:

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

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

Row_array:

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

if ($query->num_rows() > 0)
{
   $row = $query->row_array(); 

   echo $row['title'];
   echo $row['name'];
   echo $row['body'];
}
like image 102
Thomas Lomas Avatar answered Oct 19 '22 22:10

Thomas Lomas


  1. result_array()

    Returns the query result as a pure array. Typically you’ll use this in a foreach loop.

  2. row_array()

    Returns a single result row. If your query has more than one row, it returns only the first row.
    Identical to the row() method, except it returns an array.

like image 2
karthikeyan Avatar answered Oct 19 '22 22:10

karthikeyan