phparrayscodeigniter

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();
}

Solution

  • 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'];
    }