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();
}
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'];
}
result_array()
Returns the query result as a pure array. Typically you’ll use this in a foreach
loop.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With