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 | |
James L Parry | 014bc89 | 2014-11-24 17:14:19 -0800 | [diff] [blame] | 7 | ************* |
| 8 | Result Arrays |
| 9 | ************* |
| 10 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 11 | result() |
| 12 | ======== |
| 13 | |
James L Parry | e4a9f64 | 2014-11-24 16:20:53 -0800 | [diff] [blame] | 14 | This method returns the query result as an array of **objects**, or |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 15 | **an empty array** on failure. Typically you'll use this in a foreach |
| 16 | loop, like this:: |
| 17 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 18 | $query = $this->db->query("YOUR QUERY"); |
| 19 | |
| 20 | foreach ($query->result() as $row) |
| 21 | { |
| 22 | echo $row->title; |
| 23 | echo $row->name; |
| 24 | echo $row->body; |
| 25 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 26 | |
James L Parry | e4a9f64 | 2014-11-24 16:20:53 -0800 | [diff] [blame] | 27 | The above method is an alias of result_object(). |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 28 | |
| 29 | If you run queries that might **not** produce a result, you are |
| 30 | encouraged to test the result first:: |
| 31 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 32 | $query = $this->db->query("YOUR QUERY"); |
| 33 | |
| 34 | if ($query->num_rows() > 0) |
| 35 | { |
| 36 | foreach ($query->result() as $row) |
| 37 | { |
| 38 | echo $row->title; |
| 39 | echo $row->name; |
| 40 | echo $row->body; |
| 41 | } |
| 42 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 43 | |
| 44 | You can also pass a string to result() which represents a class to |
| 45 | instantiate for each result object (note: this class must be loaded) |
| 46 | |
| 47 | :: |
| 48 | |
| 49 | $query = $this->db->query("SELECT * FROM users;"); |
| 50 | |
| 51 | foreach ($query->result('User') as $user) |
| 52 | { |
| 53 | echo $user->name; // call attributes |
| 54 | echo $user->reverse_name(); // or methods defined on the 'User' class |
| 55 | } |
| 56 | |
| 57 | result_array() |
| 58 | =============== |
| 59 | |
James L Parry | e4a9f64 | 2014-11-24 16:20:53 -0800 | [diff] [blame] | 60 | This method returns the query result as a pure array, or an empty |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 61 | array when no result is produced. Typically you'll use this in a foreach |
| 62 | loop, like this:: |
| 63 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 64 | $query = $this->db->query("YOUR QUERY"); |
| 65 | |
| 66 | foreach ($query->result_array() as $row) |
| 67 | { |
| 68 | echo $row['title']; |
| 69 | echo $row['name']; |
| 70 | echo $row['body']; |
| 71 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 72 | |
James L Parry | 014bc89 | 2014-11-24 17:14:19 -0800 | [diff] [blame] | 73 | *********** |
| 74 | Result Rows |
| 75 | *********** |
| 76 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 77 | row() |
| 78 | ===== |
| 79 | |
James L Parry | e4a9f64 | 2014-11-24 16:20:53 -0800 | [diff] [blame] | 80 | This method returns a single result row. If your query has more than |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 81 | one row, it returns only the first row. The result is returned as an |
| 82 | **object**. Here's a usage example:: |
| 83 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 84 | $query = $this->db->query("YOUR QUERY"); |
| 85 | |
| 86 | if ($query->num_rows() > 0) |
| 87 | { |
| 88 | $row = $query->row(); |
| 89 | |
| 90 | echo $row->title; |
| 91 | echo $row->name; |
| 92 | echo $row->body; |
| 93 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 94 | |
| 95 | If you want a specific row returned you can submit the row number as a |
| 96 | digit in the first parameter:: |
| 97 | |
| 98 | $row = $query->row(5); |
| 99 | |
| 100 | You can also add a second String parameter, which is the name of a class |
| 101 | to instantiate the row with:: |
| 102 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 103 | $query = $this->db->query("SELECT * FROM users LIMIT 1;"); |
| 104 | $query->row(0, 'User'); |
| 105 | |
| 106 | echo $row->name; // call attributes |
| 107 | echo $row->reverse_name(); // or methods defined on the 'User' class |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 108 | |
| 109 | row_array() |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 110 | =========== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 111 | |
James L Parry | e4a9f64 | 2014-11-24 16:20:53 -0800 | [diff] [blame] | 112 | Identical to the above row() method, except it returns an array. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 113 | Example:: |
| 114 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 115 | $query = $this->db->query("YOUR QUERY"); |
| 116 | |
| 117 | if ($query->num_rows() > 0) |
| 118 | { |
| 119 | $row = $query->row_array(); |
| 120 | |
| 121 | echo $row['title']; |
| 122 | echo $row['name']; |
| 123 | echo $row['body']; |
| 124 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 125 | |
| 126 | If you want a specific row returned you can submit the row number as a |
| 127 | digit in the first parameter:: |
| 128 | |
| 129 | $row = $query->row_array(5); |
| 130 | |
| 131 | In addition, you can walk forward/backwards/first/last through your |
| 132 | results using these variations: |
| 133 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 134 | | **$row = $query->first_row()** |
| 135 | | **$row = $query->last_row()** |
| 136 | | **$row = $query->next_row()** |
| 137 | | **$row = $query->previous_row()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 138 | |
| 139 | By default they return an object unless you put the word "array" in the |
| 140 | parameter: |
| 141 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 142 | | **$row = $query->first_row('array')** |
| 143 | | **$row = $query->last_row('array')** |
| 144 | | **$row = $query->next_row('array')** |
| 145 | | **$row = $query->previous_row('array')** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 146 | |
James L Parry | 8b85526 | 2014-11-24 17:29:04 -0800 | [diff] [blame] | 147 | .. note:: all the methods above will load the whole result into memory |
| 148 | (prefetching) use unbuffered_row() for processing large result sets. |
Juan Ignacio Borda | d981e29 | 2012-05-18 18:29:24 -0300 | [diff] [blame] | 149 | |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 150 | unbuffered_row() |
| 151 | ================ |
Juan Ignacio Borda | d981e29 | 2012-05-18 18:29:24 -0300 | [diff] [blame] | 152 | |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 153 | This method returns a single result row without prefetching the whole |
| 154 | result in memory as ``row()`` does. If your query has more than one row, |
| 155 | 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] | 156 | |
Derek Jones | ce79be0 | 2012-06-25 23:23:46 -0700 | [diff] [blame] | 157 | :: |
Juan Ignacio Borda | d981e29 | 2012-05-18 18:29:24 -0300 | [diff] [blame] | 158 | |
| 159 | $query = $this->db->query("YOUR QUERY"); |
| 160 | |
Juan Ignacio Borda | da7c9e0 | 2012-05-19 09:42:40 -0300 | [diff] [blame] | 161 | while ($row = $query->unbuffered_row()) |
Juan Ignacio Borda | d981e29 | 2012-05-18 18:29:24 -0300 | [diff] [blame] | 162 | { |
| 163 | echo $row->title; |
| 164 | echo $row->name; |
| 165 | echo $row->body; |
| 166 | } |
| 167 | |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 168 | You can optionally pass 'object' (default) or 'array' in order to specify |
| 169 | the returned value's type:: |
| 170 | |
| 171 | $query->unbuffered_row(); // object |
| 172 | $query->unbuffered_row('object'); // object |
| 173 | $query->unbuffered_row('array'); // associative array |
| 174 | |
James L Parry | e4a9f64 | 2014-11-24 16:20:53 -0800 | [diff] [blame] | 175 | ********************* |
| 176 | Result Helper Methods |
| 177 | ********************* |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 178 | |
James L Parry | e4a9f64 | 2014-11-24 16:20:53 -0800 | [diff] [blame] | 179 | **$query->num_rows()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 180 | |
| 181 | The number of rows returned by the query. Note: In this example, $query |
| 182 | is the variable that the query result object is assigned to:: |
| 183 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 184 | $query = $this->db->query('SELECT * FROM my_table'); |
| 185 | |
| 186 | echo $query->num_rows(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 187 | |
Timothy Warren | 69bb408 | 2012-03-05 12:49:55 -0500 | [diff] [blame] | 188 | .. note:: |
Andrey Andreev | fdb7541 | 2012-03-05 16:32:17 +0200 | [diff] [blame] | 189 | Not all database drivers have a native way of getting the total |
| 190 | number of rows for a result set. When this is the case, all of |
| 191 | the data is prefetched and count() is manually called on the |
James L Parry | e4a9f64 | 2014-11-24 16:20:53 -0800 | [diff] [blame] | 192 | resulting array in order to achieve the same methodality. |
Andrey Andreev | fdb7541 | 2012-03-05 16:32:17 +0200 | [diff] [blame] | 193 | |
James L Parry | e4a9f64 | 2014-11-24 16:20:53 -0800 | [diff] [blame] | 194 | **$query->num_fields()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 195 | |
| 196 | The number of FIELDS (columns) returned by the query. Make sure to call |
James L Parry | e4a9f64 | 2014-11-24 16:20:53 -0800 | [diff] [blame] | 197 | the method using your query result object:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 198 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 199 | $query = $this->db->query('SELECT * FROM my_table'); |
| 200 | |
| 201 | echo $query->num_fields(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 202 | |
James L Parry | e4a9f64 | 2014-11-24 16:20:53 -0800 | [diff] [blame] | 203 | **$query->free_result()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 204 | |
| 205 | It frees the memory associated with the result and deletes the result |
| 206 | resource ID. Normally PHP frees its memory automatically at the end of |
| 207 | script execution. However, if you are running a lot of queries in a |
| 208 | particular script you might want to free the result after each query |
| 209 | result has been generated in order to cut down on memory consumptions. |
| 210 | Example:: |
| 211 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 212 | $query = $this->db->query('SELECT title FROM my_table'); |
| 213 | |
| 214 | foreach ($query->result() as $row) |
| 215 | { |
| 216 | echo $row->title; |
| 217 | } |
| 218 | $query->free_result(); // The $query result object will no longer be available |
| 219 | |
| 220 | $query2 = $this->db->query('SELECT name FROM some_table'); |
| 221 | |
| 222 | $row = $query2->row(); |
| 223 | echo $row->name; |
Andrey Andreev | fdb7541 | 2012-03-05 16:32:17 +0200 | [diff] [blame] | 224 | $query2->free_result(); // The $query2 result object will no longer be available |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 225 | |
James L Parry | e4a9f64 | 2014-11-24 16:20:53 -0800 | [diff] [blame] | 226 | **data_seek()** |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 227 | |
| 228 | This method sets the internal pointer for the next result row to be |
| 229 | fetched. It is only useful in combination with ``unbuffered_row()``. |
| 230 | |
| 231 | It accepts a positive integer value, which defaults to 0 and returns |
| 232 | TRUE on success or FALSE on failure. |
| 233 | |
| 234 | :: |
| 235 | |
| 236 | $query = $this->db->query('SELECT `field_name` FROM `table_name`'); |
| 237 | $query->data_seek(5); // Skip the first 5 rows |
| 238 | $row = $query->unbuffered_row(); |
| 239 | |
| 240 | .. note:: Not all database drivers support this feature and will return FALSE. |
| 241 | Most notably - you won't be able to use it with PDO. |