Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ######################## |
| 2 | Generating Query Results |
| 3 | ######################## |
| 4 | |
| 5 | There are several ways to generate query results: |
| 6 | |
| 7 | result() |
| 8 | ======== |
| 9 | |
| 10 | This function returns the query result as an array of **objects**, or |
| 11 | **an empty array** on failure. Typically you'll use this in a foreach |
| 12 | loop, like this:: |
| 13 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 14 | $query = $this->db->query("YOUR QUERY"); |
| 15 | |
| 16 | foreach ($query->result() as $row) |
| 17 | { |
| 18 | echo $row->title; |
| 19 | echo $row->name; |
| 20 | echo $row->body; |
| 21 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 22 | |
| 23 | The above function is an alias of result_object(). |
| 24 | |
| 25 | If you run queries that might **not** produce a result, you are |
| 26 | encouraged to test the result first:: |
| 27 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 28 | $query = $this->db->query("YOUR QUERY"); |
| 29 | |
| 30 | if ($query->num_rows() > 0) |
| 31 | { |
| 32 | foreach ($query->result() as $row) |
| 33 | { |
| 34 | echo $row->title; |
| 35 | echo $row->name; |
| 36 | echo $row->body; |
| 37 | } |
| 38 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 39 | |
| 40 | You can also pass a string to result() which represents a class to |
| 41 | instantiate for each result object (note: this class must be loaded) |
| 42 | |
| 43 | :: |
| 44 | |
| 45 | $query = $this->db->query("SELECT * FROM users;"); |
| 46 | |
| 47 | foreach ($query->result('User') as $user) |
| 48 | { |
| 49 | echo $user->name; // call attributes |
| 50 | echo $user->reverse_name(); // or methods defined on the 'User' class |
| 51 | } |
| 52 | |
| 53 | result_array() |
| 54 | =============== |
| 55 | |
| 56 | This function returns the query result as a pure array, or an empty |
| 57 | array when no result is produced. Typically you'll use this in a foreach |
| 58 | loop, like this:: |
| 59 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 60 | $query = $this->db->query("YOUR QUERY"); |
| 61 | |
| 62 | foreach ($query->result_array() as $row) |
| 63 | { |
| 64 | echo $row['title']; |
| 65 | echo $row['name']; |
| 66 | echo $row['body']; |
| 67 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 68 | |
| 69 | row() |
| 70 | ===== |
| 71 | |
| 72 | This function returns a single result row. If your query has more than |
| 73 | one row, it returns only the first row. The result is returned as an |
| 74 | **object**. Here's a usage example:: |
| 75 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 76 | $query = $this->db->query("YOUR QUERY"); |
| 77 | |
| 78 | if ($query->num_rows() > 0) |
| 79 | { |
| 80 | $row = $query->row(); |
| 81 | |
| 82 | echo $row->title; |
| 83 | echo $row->name; |
| 84 | echo $row->body; |
| 85 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 86 | |
| 87 | If you want a specific row returned you can submit the row number as a |
| 88 | digit in the first parameter:: |
| 89 | |
| 90 | $row = $query->row(5); |
| 91 | |
| 92 | You can also add a second String parameter, which is the name of a class |
| 93 | to instantiate the row with:: |
| 94 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 95 | $query = $this->db->query("SELECT * FROM users LIMIT 1;"); |
| 96 | $query->row(0, 'User'); |
| 97 | |
| 98 | echo $row->name; // call attributes |
| 99 | echo $row->reverse_name(); // or methods defined on the 'User' class |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 100 | |
| 101 | row_array() |
| 102 | ============ |
| 103 | |
| 104 | Identical to the above row() function, except it returns an array. |
| 105 | Example:: |
| 106 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 107 | $query = $this->db->query("YOUR QUERY"); |
| 108 | |
| 109 | if ($query->num_rows() > 0) |
| 110 | { |
| 111 | $row = $query->row_array(); |
| 112 | |
| 113 | echo $row['title']; |
| 114 | echo $row['name']; |
| 115 | echo $row['body']; |
| 116 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 117 | |
| 118 | If you want a specific row returned you can submit the row number as a |
| 119 | digit in the first parameter:: |
| 120 | |
| 121 | $row = $query->row_array(5); |
| 122 | |
| 123 | In addition, you can walk forward/backwards/first/last through your |
| 124 | results using these variations: |
| 125 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 126 | | **$row = $query->first_row()** |
| 127 | | **$row = $query->last_row()** |
| 128 | | **$row = $query->next_row()** |
| 129 | | **$row = $query->previous_row()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 130 | |
| 131 | By default they return an object unless you put the word "array" in the |
| 132 | parameter: |
| 133 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 134 | | **$row = $query->first_row('array')** |
| 135 | | **$row = $query->last_row('array')** |
| 136 | | **$row = $query->next_row('array')** |
| 137 | | **$row = $query->previous_row('array')** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 138 | |
| 139 | *********************** |
| 140 | Result Helper Functions |
| 141 | *********************** |
| 142 | |
| 143 | $query->num_rows() |
| 144 | =================== |
| 145 | |
| 146 | The number of rows returned by the query. Note: In this example, $query |
| 147 | is the variable that the query result object is assigned to:: |
| 148 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 149 | $query = $this->db->query('SELECT * FROM my_table'); |
| 150 | |
| 151 | echo $query->num_rows(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 152 | |
| 153 | $query->num_fields() |
| 154 | ===================== |
| 155 | |
| 156 | The number of FIELDS (columns) returned by the query. Make sure to call |
| 157 | the function using your query result object:: |
| 158 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 159 | $query = $this->db->query('SELECT * FROM my_table'); |
| 160 | |
| 161 | echo $query->num_fields(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 162 | |
| 163 | $query->free_result() |
| 164 | ====================== |
| 165 | |
| 166 | It frees the memory associated with the result and deletes the result |
| 167 | resource ID. Normally PHP frees its memory automatically at the end of |
| 168 | script execution. However, if you are running a lot of queries in a |
| 169 | particular script you might want to free the result after each query |
| 170 | result has been generated in order to cut down on memory consumptions. |
| 171 | Example:: |
| 172 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 173 | $query = $this->db->query('SELECT title FROM my_table'); |
| 174 | |
| 175 | foreach ($query->result() as $row) |
| 176 | { |
| 177 | echo $row->title; |
| 178 | } |
| 179 | $query->free_result(); // The $query result object will no longer be available |
| 180 | |
| 181 | $query2 = $this->db->query('SELECT name FROM some_table'); |
| 182 | |
| 183 | $row = $query2->row(); |
| 184 | echo $row->name; |
| 185 | $query2->free_result();// The $query2 result object will no longer be available |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 186 | |