Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save abdullahbutt/7524937 to your computer and use it in GitHub Desktop.

Select an option

Save abdullahbutt/7524937 to your computer and use it in GitHub Desktop.
CI_Active_Record_Displaying_Query_Results
Displaying Query Results
Showing database query results in CI is quite simple. We define our query as above, ending in:
$query = $this->db->get();
Then, if there are multiple results, they can be returned as a $row object through which you iterate with a foreach loop:
foreach ($query->result() as $row)
{
print $row->url;
print $row->name;
print $row->client;
}
or if we only want a single result, it can be returned as an object, or here as a
$row array:
if ($query->num_rows() > 0)
{
$row = $query->row_array();
print $row['url'];
print $row['name'];
print $row['client'];
}
Personally, I prefer the object syntax to the array—less typing!
When you follow the MVC pattern, you will usually want to keep your queries and database interactions in models, and display the information through views
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment