Created
November 18, 2013 09:14
-
-
Save abdullahbutt/7524937 to your computer and use it in GitHub Desktop.
CI_Active_Record_Displaying_Query_Results
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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