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() |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 102 | =========== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 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 | |
Juan Ignacio Borda | d981e29 | 2012-05-18 18:29:24 -0300 | [diff] [blame] | 139 | .. note:: all the functions above will load the whole result into memory (prefetching) use unbuffered_row() for processing large result sets. |
| 140 | |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 141 | unbuffered_row() |
| 142 | ================ |
Juan Ignacio Borda | d981e29 | 2012-05-18 18:29:24 -0300 | [diff] [blame] | 143 | |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 144 | This method returns a single result row without prefetching the whole |
| 145 | result in memory as ``row()`` does. If your query has more than one row, |
| 146 | it returns the current row and moves the internal data pointer ahead. |
Juan Ignacio Borda | d981e29 | 2012-05-18 18:29:24 -0300 | [diff] [blame] | 147 | |
Derek Jones | ce79be0 | 2012-06-25 23:23:46 -0700 | [diff] [blame] | 148 | :: |
Juan Ignacio Borda | d981e29 | 2012-05-18 18:29:24 -0300 | [diff] [blame] | 149 | |
| 150 | $query = $this->db->query("YOUR QUERY"); |
| 151 | |
Juan Ignacio Borda | da7c9e0 | 2012-05-19 09:42:40 -0300 | [diff] [blame] | 152 | while ($row = $query->unbuffered_row()) |
Juan Ignacio Borda | d981e29 | 2012-05-18 18:29:24 -0300 | [diff] [blame] | 153 | { |
| 154 | echo $row->title; |
| 155 | echo $row->name; |
| 156 | echo $row->body; |
| 157 | } |
| 158 | |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 159 | You can optionally pass 'object' (default) or 'array' in order to specify |
| 160 | the returned value's type:: |
| 161 | |
| 162 | $query->unbuffered_row(); // object |
| 163 | $query->unbuffered_row('object'); // object |
| 164 | $query->unbuffered_row('array'); // associative array |
| 165 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 166 | *********************** |
| 167 | Result Helper Functions |
| 168 | *********************** |
| 169 | |
| 170 | $query->num_rows() |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 171 | ================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 172 | |
| 173 | The number of rows returned by the query. Note: In this example, $query |
| 174 | is the variable that the query result object is assigned to:: |
| 175 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 176 | $query = $this->db->query('SELECT * FROM my_table'); |
| 177 | |
| 178 | echo $query->num_rows(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 179 | |
Timothy Warren | 69bb408 | 2012-03-05 12:49:55 -0500 | [diff] [blame] | 180 | .. note:: |
Andrey Andreev | fdb7541 | 2012-03-05 16:32:17 +0200 | [diff] [blame] | 181 | Not all database drivers have a native way of getting the total |
| 182 | number of rows for a result set. When this is the case, all of |
| 183 | the data is prefetched and count() is manually called on the |
| 184 | resulting array in order to achieve the same functionality. |
| 185 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 186 | $query->num_fields() |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 187 | ==================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 188 | |
| 189 | The number of FIELDS (columns) returned by the query. Make sure to call |
| 190 | the function using your query result object:: |
| 191 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 192 | $query = $this->db->query('SELECT * FROM my_table'); |
| 193 | |
| 194 | echo $query->num_fields(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 195 | |
| 196 | $query->free_result() |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 197 | ===================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 198 | |
| 199 | It frees the memory associated with the result and deletes the result |
| 200 | resource ID. Normally PHP frees its memory automatically at the end of |
| 201 | script execution. However, if you are running a lot of queries in a |
| 202 | particular script you might want to free the result after each query |
| 203 | result has been generated in order to cut down on memory consumptions. |
| 204 | Example:: |
| 205 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 206 | $query = $this->db->query('SELECT title FROM my_table'); |
| 207 | |
| 208 | foreach ($query->result() as $row) |
| 209 | { |
| 210 | echo $row->title; |
| 211 | } |
| 212 | $query->free_result(); // The $query result object will no longer be available |
| 213 | |
| 214 | $query2 = $this->db->query('SELECT name FROM some_table'); |
| 215 | |
| 216 | $row = $query2->row(); |
| 217 | echo $row->name; |
Andrey Andreev | fdb7541 | 2012-03-05 16:32:17 +0200 | [diff] [blame] | 218 | $query2->free_result(); // The $query2 result object will no longer be available |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 219 | |
| 220 | data_seek() |
| 221 | =========== |
| 222 | |
| 223 | This method sets the internal pointer for the next result row to be |
| 224 | fetched. It is only useful in combination with ``unbuffered_row()``. |
| 225 | |
| 226 | It accepts a positive integer value, which defaults to 0 and returns |
| 227 | TRUE on success or FALSE on failure. |
| 228 | |
| 229 | :: |
| 230 | |
| 231 | $query = $this->db->query('SELECT `field_name` FROM `table_name`'); |
| 232 | $query->data_seek(5); // Skip the first 5 rows |
| 233 | $row = $query->unbuffered_row(); |
| 234 | |
| 235 | .. note:: Not all database drivers support this feature and will return FALSE. |
| 236 | Most notably - you won't be able to use it with PDO. |