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 | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 7 | .. contents:: |
| 8 | :local: |
| 9 | :depth: 2 |
| 10 | |
James L Parry | 014bc89 | 2014-11-24 17:14:19 -0800 | [diff] [blame] | 11 | ************* |
| 12 | Result Arrays |
| 13 | ************* |
| 14 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 15 | **result()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 16 | |
James L Parry | e4a9f64 | 2014-11-24 16:20:53 -0800 | [diff] [blame] | 17 | This method returns the query result as an array of **objects**, or |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 18 | **an empty array** on failure. Typically you'll use this in a foreach |
| 19 | loop, like this:: |
| 20 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 21 | $query = $this->db->query("YOUR QUERY"); |
Andrey Andreev | 10b6e7a | 2015-07-24 12:38:58 +0300 | [diff] [blame] | 22 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 23 | foreach ($query->result() as $row) |
| 24 | { |
| 25 | echo $row->title; |
| 26 | echo $row->name; |
| 27 | echo $row->body; |
| 28 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 29 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 30 | The above method is an alias of ``result_object()``. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 31 | |
Andrey Andreev | 10b6e7a | 2015-07-24 12:38:58 +0300 | [diff] [blame] | 32 | You can also pass a string to ``result()`` which represents a class to |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 33 | instantiate for each result object (note: this class must be loaded) |
| 34 | |
| 35 | :: |
| 36 | |
| 37 | $query = $this->db->query("SELECT * FROM users;"); |
| 38 | |
| 39 | foreach ($query->result('User') as $user) |
| 40 | { |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 41 | echo $user->name; // access attributes |
| 42 | echo $user->reverse_name(); // or methods defined on the 'User' class |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 45 | **result_array()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 46 | |
James L Parry | e4a9f64 | 2014-11-24 16:20:53 -0800 | [diff] [blame] | 47 | 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] | 48 | array when no result is produced. Typically you'll use this in a foreach |
| 49 | loop, like this:: |
| 50 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 51 | $query = $this->db->query("YOUR QUERY"); |
Andrey Andreev | 10b6e7a | 2015-07-24 12:38:58 +0300 | [diff] [blame] | 52 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 53 | foreach ($query->result_array() as $row) |
| 54 | { |
| 55 | echo $row['title']; |
| 56 | echo $row['name']; |
| 57 | echo $row['body']; |
| 58 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 59 | |
James L Parry | 014bc89 | 2014-11-24 17:14:19 -0800 | [diff] [blame] | 60 | *********** |
| 61 | Result Rows |
| 62 | *********** |
| 63 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 64 | **row()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 65 | |
James L Parry | e4a9f64 | 2014-11-24 16:20:53 -0800 | [diff] [blame] | 66 | 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] | 67 | one row, it returns only the first row. The result is returned as an |
| 68 | **object**. Here's a usage example:: |
| 69 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 70 | $query = $this->db->query("YOUR QUERY"); |
Lonnie Ezell | 715959d | 2015-07-22 09:20:05 -0500 | [diff] [blame] | 71 | |
| 72 | $row = $query->row(); |
| 73 | |
Lonnie Ezell | 752f569 | 2015-07-23 23:27:01 -0500 | [diff] [blame] | 74 | if (isset($row)) |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 75 | { |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 76 | echo $row->title; |
| 77 | echo $row->name; |
| 78 | echo $row->body; |
| 79 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 80 | |
| 81 | If you want a specific row returned you can submit the row number as a |
| 82 | digit in the first parameter:: |
| 83 | |
| 84 | $row = $query->row(5); |
| 85 | |
| 86 | You can also add a second String parameter, which is the name of a class |
| 87 | to instantiate the row with:: |
| 88 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 89 | $query = $this->db->query("SELECT * FROM users LIMIT 1;"); |
sv3tli0 | d829a5f | 2015-03-02 17:22:01 +0200 | [diff] [blame] | 90 | $row = $query->row(0, 'User'); |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 91 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 92 | echo $row->name; // access attributes |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 93 | echo $row->reverse_name(); // or methods defined on the 'User' class |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 94 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 95 | **row_array()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 96 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 97 | Identical to the above ``row()`` method, except it returns an array. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 98 | Example:: |
| 99 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 100 | $query = $this->db->query("YOUR QUERY"); |
Lonnie Ezell | 715959d | 2015-07-22 09:20:05 -0500 | [diff] [blame] | 101 | |
| 102 | $row = $query->row_array(); |
| 103 | |
Lonnie Ezell | 752f569 | 2015-07-23 23:27:01 -0500 | [diff] [blame] | 104 | if (isset($row)) |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 105 | { |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 106 | echo $row['title']; |
| 107 | echo $row['name']; |
| 108 | echo $row['body']; |
| 109 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 110 | |
| 111 | If you want a specific row returned you can submit the row number as a |
| 112 | digit in the first parameter:: |
| 113 | |
| 114 | $row = $query->row_array(5); |
| 115 | |
| 116 | In addition, you can walk forward/backwards/first/last through your |
| 117 | results using these variations: |
| 118 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 119 | | **$row = $query->first_row()** |
| 120 | | **$row = $query->last_row()** |
| 121 | | **$row = $query->next_row()** |
| 122 | | **$row = $query->previous_row()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 123 | |
| 124 | By default they return an object unless you put the word "array" in the |
| 125 | parameter: |
| 126 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 127 | | **$row = $query->first_row('array')** |
| 128 | | **$row = $query->last_row('array')** |
| 129 | | **$row = $query->next_row('array')** |
| 130 | | **$row = $query->previous_row('array')** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 131 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 132 | .. note:: All the methods above will load the whole result into memory |
| 133 | (prefetching). Use ``unbuffered_row()`` for processing large |
| 134 | result sets. |
Juan Ignacio Borda | d981e29 | 2012-05-18 18:29:24 -0300 | [diff] [blame] | 135 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 136 | **unbuffered_row()** |
Juan Ignacio Borda | d981e29 | 2012-05-18 18:29:24 -0300 | [diff] [blame] | 137 | |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 138 | This method returns a single result row without prefetching the whole |
| 139 | result in memory as ``row()`` does. If your query has more than one row, |
| 140 | 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] | 141 | |
Derek Jones | ce79be0 | 2012-06-25 23:23:46 -0700 | [diff] [blame] | 142 | :: |
Juan Ignacio Borda | d981e29 | 2012-05-18 18:29:24 -0300 | [diff] [blame] | 143 | |
| 144 | $query = $this->db->query("YOUR QUERY"); |
Andrey Andreev | 10b6e7a | 2015-07-24 12:38:58 +0300 | [diff] [blame] | 145 | |
Juan Ignacio Borda | da7c9e0 | 2012-05-19 09:42:40 -0300 | [diff] [blame] | 146 | while ($row = $query->unbuffered_row()) |
Juan Ignacio Borda | d981e29 | 2012-05-18 18:29:24 -0300 | [diff] [blame] | 147 | { |
| 148 | echo $row->title; |
| 149 | echo $row->name; |
| 150 | echo $row->body; |
| 151 | } |
| 152 | |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 153 | You can optionally pass 'object' (default) or 'array' in order to specify |
| 154 | the returned value's type:: |
| 155 | |
| 156 | $query->unbuffered_row(); // object |
| 157 | $query->unbuffered_row('object'); // object |
| 158 | $query->unbuffered_row('array'); // associative array |
| 159 | |
James L Parry | e4a9f64 | 2014-11-24 16:20:53 -0800 | [diff] [blame] | 160 | ********************* |
Lonnie Ezell | cf3cc1d | 2015-07-21 10:22:41 -0500 | [diff] [blame] | 161 | Custom Result Objects |
| 162 | ********************* |
| 163 | |
Andrey Andreev | 56d3467 | 2015-07-23 14:03:26 +0300 | [diff] [blame] | 164 | You can have the results returned as an instance of a custom class instead |
| 165 | of a ``stdClass`` or array, as the ``result()`` and ``result_array()`` |
| 166 | methods allow. This requires that the class is already loaded into memory. |
| 167 | The object will have all values returned from the database set as properties. |
| 168 | If these have been declared and are non-public then you should provide a |
| 169 | ``__set()`` method to allow them to be set. |
Lonnie Ezell | cf3cc1d | 2015-07-21 10:22:41 -0500 | [diff] [blame] | 170 | |
| 171 | Example:: |
| 172 | |
| 173 | class User { |
Lonnie Ezell | 2520182 | 2015-07-21 21:32:43 -0500 | [diff] [blame] | 174 | |
Andrey Andreev | 56d3467 | 2015-07-23 14:03:26 +0300 | [diff] [blame] | 175 | public $id; |
| 176 | public $email; |
| 177 | public $username; |
| 178 | |
Lonnie Ezell | cf3cc1d | 2015-07-21 10:22:41 -0500 | [diff] [blame] | 179 | protected $last_login; |
| 180 | |
| 181 | public function last_login($format) |
| 182 | { |
Lonnie Ezell | abf089a | 2015-07-22 09:00:37 -0500 | [diff] [blame] | 183 | return $this->last_login->format($format); |
Lonnie Ezell | cf3cc1d | 2015-07-21 10:22:41 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | public function __set($name, $value) |
| 187 | { |
Lonnie Ezell | abf089a | 2015-07-22 09:00:37 -0500 | [diff] [blame] | 188 | if ($name === 'last_login') |
Lonnie Ezell | c87a66b | 2015-07-22 21:48:02 -0500 | [diff] [blame] | 189 | { |
| 190 | $this->last_login = DateTime::createFromFormat('U', $value); |
| 191 | } |
Lonnie Ezell | cf3cc1d | 2015-07-21 10:22:41 -0500 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | public function __get($name) |
| 195 | { |
| 196 | if (isset($this->$name)) |
| 197 | { |
| 198 | return $this->$name; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
Andrey Andreev | 56d3467 | 2015-07-23 14:03:26 +0300 | [diff] [blame] | 203 | In addition to the two methods listed below, the following methods also can |
| 204 | take a class name to return the results as: ``first_row()``, ``last_row()``, |
| 205 | ``next_row()``, and ``previous_row()``. |
Lonnie Ezell | cf3cc1d | 2015-07-21 10:22:41 -0500 | [diff] [blame] | 206 | |
| 207 | **custom_result_object()** |
| 208 | |
Andrey Andreev | 56d3467 | 2015-07-23 14:03:26 +0300 | [diff] [blame] | 209 | Returns the entire result set as an array of instances of the class requested. |
| 210 | The only parameter is the name of the class to instantiate. |
Lonnie Ezell | cf3cc1d | 2015-07-21 10:22:41 -0500 | [diff] [blame] | 211 | |
| 212 | Example:: |
| 213 | |
| 214 | $query = $this->db->query("YOUR QUERY"); |
| 215 | |
Lonnie Ezell | 2520182 | 2015-07-21 21:32:43 -0500 | [diff] [blame] | 216 | $rows = $query->custom_result_object('User'); |
| 217 | |
| 218 | foreach ($rows as $row) |
Lonnie Ezell | cf3cc1d | 2015-07-21 10:22:41 -0500 | [diff] [blame] | 219 | { |
Lonnie Ezell | 2520182 | 2015-07-21 21:32:43 -0500 | [diff] [blame] | 220 | echo $row->id; |
| 221 | echo $row->email; |
| 222 | echo $row->last_login('Y-m-d'); |
Lonnie Ezell | cf3cc1d | 2015-07-21 10:22:41 -0500 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | **custom_row_object()** |
| 226 | |
Andrey Andreev | 56d3467 | 2015-07-23 14:03:26 +0300 | [diff] [blame] | 227 | Returns a single row from your query results. The first parameter is the row |
| 228 | number of the results. The second parameter is the class name to instantiate. |
Lonnie Ezell | cf3cc1d | 2015-07-21 10:22:41 -0500 | [diff] [blame] | 229 | |
| 230 | Example:: |
| 231 | |
| 232 | $query = $this->db->query("YOUR QUERY"); |
| 233 | |
Lonnie Ezell | c87a66b | 2015-07-22 21:48:02 -0500 | [diff] [blame] | 234 | $row = $query->custom_row_object(0, 'User'); |
Lonnie Ezell | cf3cc1d | 2015-07-21 10:22:41 -0500 | [diff] [blame] | 235 | |
Andrey Andreev | 56d3467 | 2015-07-23 14:03:26 +0300 | [diff] [blame] | 236 | if (isset($row)) |
Lonnie Ezell | c87a66b | 2015-07-22 21:48:02 -0500 | [diff] [blame] | 237 | { |
Lonnie Ezell | cf3cc1d | 2015-07-21 10:22:41 -0500 | [diff] [blame] | 238 | echo $row->email; // access attributes |
| 239 | echo $row->last_login('Y-m-d'); // access class methods |
| 240 | } |
| 241 | |
| 242 | You can also use the ``row()`` method in exactly the same way. |
| 243 | |
| 244 | Example:: |
| 245 | |
Lonnie Ezell | 2520182 | 2015-07-21 21:32:43 -0500 | [diff] [blame] | 246 | $row = $query->custom_row_object(0, 'User'); |
Lonnie Ezell | cf3cc1d | 2015-07-21 10:22:41 -0500 | [diff] [blame] | 247 | |
| 248 | ********************* |
James L Parry | e4a9f64 | 2014-11-24 16:20:53 -0800 | [diff] [blame] | 249 | Result Helper Methods |
| 250 | ********************* |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 251 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 252 | **num_rows()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 253 | |
| 254 | The number of rows returned by the query. Note: In this example, $query |
| 255 | is the variable that the query result object is assigned to:: |
| 256 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 257 | $query = $this->db->query('SELECT * FROM my_table'); |
Andrey Andreev | 10b6e7a | 2015-07-24 12:38:58 +0300 | [diff] [blame] | 258 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 259 | echo $query->num_rows(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 260 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 261 | .. note:: Not all database drivers have a native way of getting the total |
Andrey Andreev | fdb7541 | 2012-03-05 16:32:17 +0200 | [diff] [blame] | 262 | number of rows for a result set. When this is the case, all of |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 263 | the data is prefetched and ``count()`` is manually called on the |
| 264 | resulting array in order to achieve the same result. |
Andrey Andreev | fdb7541 | 2012-03-05 16:32:17 +0200 | [diff] [blame] | 265 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 266 | **num_fields()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 267 | |
| 268 | 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] | 269 | the method using your query result object:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 270 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 271 | $query = $this->db->query('SELECT * FROM my_table'); |
Andrey Andreev | 10b6e7a | 2015-07-24 12:38:58 +0300 | [diff] [blame] | 272 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 273 | echo $query->num_fields(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 274 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 275 | **free_result()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 276 | |
| 277 | It frees the memory associated with the result and deletes the result |
| 278 | resource ID. Normally PHP frees its memory automatically at the end of |
| 279 | script execution. However, if you are running a lot of queries in a |
| 280 | particular script you might want to free the result after each query |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 281 | result has been generated in order to cut down on memory consumption. |
| 282 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 283 | Example:: |
| 284 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 285 | $query = $this->db->query('SELECT title FROM my_table'); |
Andrey Andreev | 10b6e7a | 2015-07-24 12:38:58 +0300 | [diff] [blame] | 286 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 287 | foreach ($query->result() as $row) |
| 288 | { |
| 289 | echo $row->title; |
| 290 | } |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 291 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 292 | $query->free_result(); // The $query result object will no longer be available |
| 293 | |
| 294 | $query2 = $this->db->query('SELECT name FROM some_table'); |
| 295 | |
| 296 | $row = $query2->row(); |
| 297 | echo $row->name; |
Andrey Andreev | fdb7541 | 2012-03-05 16:32:17 +0200 | [diff] [blame] | 298 | $query2->free_result(); // The $query2 result object will no longer be available |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 299 | |
James L Parry | e4a9f64 | 2014-11-24 16:20:53 -0800 | [diff] [blame] | 300 | **data_seek()** |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 301 | |
| 302 | This method sets the internal pointer for the next result row to be |
| 303 | fetched. It is only useful in combination with ``unbuffered_row()``. |
| 304 | |
| 305 | It accepts a positive integer value, which defaults to 0 and returns |
| 306 | TRUE on success or FALSE on failure. |
| 307 | |
| 308 | :: |
| 309 | |
| 310 | $query = $this->db->query('SELECT `field_name` FROM `table_name`'); |
| 311 | $query->data_seek(5); // Skip the first 5 rows |
| 312 | $row = $query->unbuffered_row(); |
| 313 | |
| 314 | .. note:: Not all database drivers support this feature and will return FALSE. |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 315 | Most notably - you won't be able to use it with PDO. |
| 316 | |
| 317 | *************** |
| 318 | Class Reference |
| 319 | *************** |
| 320 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 321 | .. php:class:: CI_DB_result |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 322 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 323 | .. php:method:: result([$type = 'object']) |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 324 | |
| 325 | :param string $type: Type of requested results - array, object, or class name |
| 326 | :returns: Array containing the fetched rows |
| 327 | :rtype: array |
| 328 | |
| 329 | A wrapper for the ``result_array()``, ``result_object()`` |
| 330 | and ``custom_result_object()`` methods. |
| 331 | |
| 332 | Usage: see `Result Arrays`_. |
| 333 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 334 | .. php:method:: result_array() |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 335 | |
| 336 | :returns: Array containing the fetched rows |
| 337 | :rtype: array |
| 338 | |
| 339 | Returns the query results as an array of rows, where each |
| 340 | row is itself an associative array. |
| 341 | |
| 342 | Usage: see `Result Arrays`_. |
| 343 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 344 | .. php:method:: result_object() |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 345 | |
| 346 | :returns: Array containing the fetched rows |
| 347 | :rtype: array |
| 348 | |
| 349 | Returns the query results as an array of rows, where each |
| 350 | row is an object of type ``stdClass``. |
| 351 | |
| 352 | Usage: see `Result Arrays`_. |
| 353 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 354 | .. php:method:: custom_result_object($class_name) |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 355 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 356 | :param string $class_name: Class name for the resulting rows |
| 357 | :returns: Array containing the fetched rows |
| 358 | :rtype: array |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 359 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 360 | Returns the query results as an array of rows, where each |
| 361 | row is an instance of the specified class. |
| 362 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 363 | .. php:method:: row([$n = 0[, $type = 'object']]) |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 364 | |
| 365 | :param int $n: Index of the query results row to be returned |
| 366 | :param string $type: Type of the requested result - array, object, or class name |
| 367 | :returns: The requested row or NULL if it doesn't exist |
| 368 | :rtype: mixed |
| 369 | |
| 370 | A wrapper for the ``row_array()``, ``row_object() and |
| 371 | ``custom_row_object()`` methods. |
| 372 | |
| 373 | Usage: see `Result Rows`_. |
| 374 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 375 | .. php:method:: unbuffered_row([$type = 'object']) |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 376 | |
| 377 | :param string $type: Type of the requested result - array, object, or class name |
| 378 | :returns: Next row from the result set or NULL if it doesn't exist |
| 379 | :rtype: mixed |
| 380 | |
| 381 | Fetches the next result row and returns it in the |
| 382 | requested form. |
| 383 | |
| 384 | Usage: see `Result Rows`_. |
| 385 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 386 | .. php:method:: row_array([$n = 0]) |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 387 | |
| 388 | :param int $n: Index of the query results row to be returned |
| 389 | :returns: The requested row or NULL if it doesn't exist |
| 390 | :rtype: array |
| 391 | |
| 392 | Returns the requested result row as an associative array. |
| 393 | |
| 394 | Usage: see `Result Rows`_. |
| 395 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 396 | .. php:method:: row_object([$n = 0]) |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 397 | |
| 398 | :param int $n: Index of the query results row to be returned |
| 399 | :returns: The requested row or NULL if it doesn't exist |
| 400 | :rtype: stdClass |
| 401 | |
| 402 | Returns the requested result row as an object of type |
| 403 | ``stdClass``. |
| 404 | |
| 405 | Usage: see `Result Rows`_. |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 406 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 407 | .. php:method:: custom_row_object($n, $type) |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 408 | |
| 409 | :param int $n: Index of the results row to return |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 410 | :param string $class_name: Class name for the resulting row |
| 411 | :returns: The requested row or NULL if it doesn't exist |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 412 | :rtype: $type |
| 413 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 414 | Returns the requested result row as an instance of the |
| 415 | requested class. |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 416 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 417 | .. php:method:: data_seek([$n = 0]) |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 418 | |
| 419 | :param int $n: Index of the results row to be returned next |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 420 | :returns: TRUE on success, FALSE on failure |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 421 | :rtype: bool |
| 422 | |
| 423 | Moves the internal results row pointer to the desired offset. |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 424 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 425 | Usage: see `Result Helper Methods`_. |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 426 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 427 | .. php:method:: set_row($key[, $value = NULL]) |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 428 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 429 | :param mixed $key: Column name or array of key/value pairs |
| 430 | :param mixed $value: Value to assign to the column, $key is a single field name |
| 431 | :rtype: void |
| 432 | |
| 433 | Assigns a value to a particular column. |
| 434 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 435 | .. php:method:: next_row([$type = 'object']) |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 436 | |
| 437 | :param string $type: Type of the requested result - array, object, or class name |
| 438 | :returns: Next row of result set, or NULL if it doesn't exist |
| 439 | :rtype: mixed |
| 440 | |
| 441 | Returns the next row from the result set. |
| 442 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 443 | .. php:method:: previous_row([$type = 'object']) |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 444 | |
| 445 | :param string $type: Type of the requested result - array, object, or class name |
| 446 | :returns: Previous row of result set, or NULL if it doesn't exist |
| 447 | :rtype: mixed |
| 448 | |
| 449 | Returns the previous row from the result set. |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 450 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 451 | .. php:method:: first_row([$type = 'object']) |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 452 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 453 | :param string $type: Type of the requested result - array, object, or class name |
| 454 | :returns: First row of result set, or NULL if it doesn't exist |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 455 | :rtype: mixed |
| 456 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 457 | Returns the first row from the result set. |
| 458 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 459 | .. php:method:: last_row([$type = 'object']) |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 460 | |
| 461 | :param string $type: Type of the requested result - array, object, or class name |
| 462 | :returns: Last row of result set, or NULL if it doesn't exist |
| 463 | :rtype: mixed |
| 464 | |
| 465 | Returns the last row from the result set. |
| 466 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 467 | .. php:method:: num_rows() |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 468 | |
| 469 | :returns: Number of rows in the result set |
| 470 | :rtype: int |
| 471 | |
| 472 | Returns the number of rows in the result set. |
| 473 | |
| 474 | Usage: see `Result Helper Methods`_. |
| 475 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 476 | .. php:method:: num_fields() |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 477 | |
| 478 | :returns: Number of fields in the result set |
| 479 | :rtype: int |
| 480 | |
| 481 | Returns the number of fields in the result set. |
| 482 | |
| 483 | Usage: see `Result Helper Methods`_. |
| 484 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 485 | .. php:method:: field_data() |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 486 | |
| 487 | :returns: Array containing field meta-data |
| 488 | :rtype: array |
| 489 | |
| 490 | Generates an array of ``stdClass`` objects containing |
| 491 | field meta-data. |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 492 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 493 | .. php:method:: free_result() |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 494 | |
| 495 | :rtype: void |
| 496 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 497 | Frees a result set. |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 498 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 499 | Usage: see `Result Helper Methods`_. |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 500 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 501 | .. php:method:: list_fields() |
James L Parry | 1516219 | 2014-12-05 23:40:47 -0800 | [diff] [blame] | 502 | |
| 503 | :returns: Array of column names |
| 504 | :rtype: array |
| 505 | |
Andrey Andreev | ab0034b | 2014-12-11 18:31:51 +0200 | [diff] [blame] | 506 | Returns an array containing the field names in the |
sv3tli0 | d829a5f | 2015-03-02 17:22:01 +0200 | [diff] [blame] | 507 | result set. |