blob: 689fd52ff8404875096a208cf13cbcf1a6bc8a84 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001########################
2Generating Query Results
3########################
4
5There are several ways to generate query results:
6
James L Parry15162192014-12-05 23:40:47 -08007.. contents::
8 :local:
9 :depth: 2
10
James L Parry014bc892014-11-24 17:14:19 -080011*************
12Result Arrays
13*************
14
Andrey Andreevab0034b2014-12-11 18:31:51 +020015**result()**
Derek Jones8ede1a22011-10-05 13:34:52 -050016
James L Parrye4a9f642014-11-24 16:20:53 -080017This method returns the query result as an array of **objects**, or
Derek Jones8ede1a22011-10-05 13:34:52 -050018**an empty array** on failure. Typically you'll use this in a foreach
19loop, like this::
20
Joseph Wensleyf24f4042011-10-06 22:53:29 -040021 $query = $this->db->query("YOUR QUERY");
22
23 foreach ($query->result() as $row)
24 {
25 echo $row->title;
26 echo $row->name;
27 echo $row->body;
28 }
Derek Jones8ede1a22011-10-05 13:34:52 -050029
Andrey Andreevab0034b2014-12-11 18:31:51 +020030The above method is an alias of ``result_object()``.
Derek Jones8ede1a22011-10-05 13:34:52 -050031
32If you run queries that might **not** produce a result, you are
33encouraged to test the result first::
34
Joseph Wensleyf24f4042011-10-06 22:53:29 -040035 $query = $this->db->query("YOUR QUERY");
Lonnie Ezell715959d2015-07-22 09:20:05 -050036
37 foreach ($query->result() as $row)
Joseph Wensleyf24f4042011-10-06 22:53:29 -040038 {
Lonnie Ezell715959d2015-07-22 09:20:05 -050039 echo $row->title;
40 echo $row->name;
41 echo $row->body;
Joseph Wensleyf24f4042011-10-06 22:53:29 -040042 }
Derek Jones8ede1a22011-10-05 13:34:52 -050043
44You can also pass a string to result() which represents a class to
45instantiate 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 {
Andrey Andreevab0034b2014-12-11 18:31:51 +020053 echo $user->name; // access attributes
54 echo $user->reverse_name(); // or methods defined on the 'User' class
Derek Jones8ede1a22011-10-05 13:34:52 -050055 }
56
Andrey Andreevab0034b2014-12-11 18:31:51 +020057**result_array()**
Derek Jones8ede1a22011-10-05 13:34:52 -050058
James L Parrye4a9f642014-11-24 16:20:53 -080059This method returns the query result as a pure array, or an empty
Derek Jones8ede1a22011-10-05 13:34:52 -050060array when no result is produced. Typically you'll use this in a foreach
61loop, like this::
62
Joseph Wensleyf24f4042011-10-06 22:53:29 -040063 $query = $this->db->query("YOUR QUERY");
64
65 foreach ($query->result_array() as $row)
66 {
67 echo $row['title'];
68 echo $row['name'];
69 echo $row['body'];
70 }
Derek Jones8ede1a22011-10-05 13:34:52 -050071
James L Parry014bc892014-11-24 17:14:19 -080072***********
73Result Rows
74***********
75
Andrey Andreevab0034b2014-12-11 18:31:51 +020076**row()**
Derek Jones8ede1a22011-10-05 13:34:52 -050077
James L Parrye4a9f642014-11-24 16:20:53 -080078This method returns a single result row. If your query has more than
Derek Jones8ede1a22011-10-05 13:34:52 -050079one row, it returns only the first row. The result is returned as an
80**object**. Here's a usage example::
81
Joseph Wensleyf24f4042011-10-06 22:53:29 -040082 $query = $this->db->query("YOUR QUERY");
Lonnie Ezell715959d2015-07-22 09:20:05 -050083
84 $row = $query->row();
85
Lonnie Ezell752f5692015-07-23 23:27:01 -050086 if (isset($row))
Joseph Wensleyf24f4042011-10-06 22:53:29 -040087 {
Joseph Wensleyf24f4042011-10-06 22:53:29 -040088 echo $row->title;
89 echo $row->name;
90 echo $row->body;
91 }
Derek Jones8ede1a22011-10-05 13:34:52 -050092
93If you want a specific row returned you can submit the row number as a
94digit in the first parameter::
95
96 $row = $query->row(5);
97
98You can also add a second String parameter, which is the name of a class
99to instantiate the row with::
100
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400101 $query = $this->db->query("SELECT * FROM users LIMIT 1;");
sv3tli0d829a5f2015-03-02 17:22:01 +0200102 $row = $query->row(0, 'User');
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400103
Andrey Andreevab0034b2014-12-11 18:31:51 +0200104 echo $row->name; // access attributes
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400105 echo $row->reverse_name(); // or methods defined on the 'User' class
Derek Jones8ede1a22011-10-05 13:34:52 -0500106
Andrey Andreevab0034b2014-12-11 18:31:51 +0200107**row_array()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500108
Andrey Andreevab0034b2014-12-11 18:31:51 +0200109Identical to the above ``row()`` method, except it returns an array.
Derek Jones8ede1a22011-10-05 13:34:52 -0500110Example::
111
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400112 $query = $this->db->query("YOUR QUERY");
Lonnie Ezell715959d2015-07-22 09:20:05 -0500113
114 $row = $query->row_array();
115
Lonnie Ezell752f5692015-07-23 23:27:01 -0500116 if (isset($row))
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400117 {
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400118 echo $row['title'];
119 echo $row['name'];
120 echo $row['body'];
121 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500122
123If you want a specific row returned you can submit the row number as a
124digit in the first parameter::
125
126 $row = $query->row_array(5);
127
128In addition, you can walk forward/backwards/first/last through your
129results using these variations:
130
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400131 | **$row = $query->first_row()**
132 | **$row = $query->last_row()**
133 | **$row = $query->next_row()**
134 | **$row = $query->previous_row()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500135
136By default they return an object unless you put the word "array" in the
137parameter:
138
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400139 | **$row = $query->first_row('array')**
140 | **$row = $query->last_row('array')**
141 | **$row = $query->next_row('array')**
142 | **$row = $query->previous_row('array')**
Derek Jones8ede1a22011-10-05 13:34:52 -0500143
Andrey Andreevab0034b2014-12-11 18:31:51 +0200144.. note:: All the methods above will load the whole result into memory
145 (prefetching). Use ``unbuffered_row()`` for processing large
146 result sets.
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300147
Andrey Andreevab0034b2014-12-11 18:31:51 +0200148**unbuffered_row()**
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300149
Andrey Andreev69edc432012-12-04 13:32:16 +0200150This method returns a single result row without prefetching the whole
151result in memory as ``row()`` does. If your query has more than one row,
152it returns the current row and moves the internal data pointer ahead.
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300153
Derek Jonesce79be02012-06-25 23:23:46 -0700154::
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300155
156 $query = $this->db->query("YOUR QUERY");
157
Juan Ignacio Bordada7c9e02012-05-19 09:42:40 -0300158 while ($row = $query->unbuffered_row())
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300159 {
160 echo $row->title;
161 echo $row->name;
162 echo $row->body;
163 }
164
Andrey Andreev69edc432012-12-04 13:32:16 +0200165You can optionally pass 'object' (default) or 'array' in order to specify
166the returned value's type::
167
168 $query->unbuffered_row(); // object
169 $query->unbuffered_row('object'); // object
170 $query->unbuffered_row('array'); // associative array
171
James L Parrye4a9f642014-11-24 16:20:53 -0800172*********************
173Result Helper Methods
174*********************
Derek Jones8ede1a22011-10-05 13:34:52 -0500175
Andrey Andreevab0034b2014-12-11 18:31:51 +0200176**num_rows()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500177
178The number of rows returned by the query. Note: In this example, $query
179is the variable that the query result object is assigned to::
180
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400181 $query = $this->db->query('SELECT * FROM my_table');
182
183 echo $query->num_rows();
Derek Jones8ede1a22011-10-05 13:34:52 -0500184
Andrey Andreevab0034b2014-12-11 18:31:51 +0200185.. note:: Not all database drivers have a native way of getting the total
Andrey Andreevfdb75412012-03-05 16:32:17 +0200186 number of rows for a result set. When this is the case, all of
Andrey Andreevab0034b2014-12-11 18:31:51 +0200187 the data is prefetched and ``count()`` is manually called on the
188 resulting array in order to achieve the same result.
Andrey Andreevfdb75412012-03-05 16:32:17 +0200189
Andrey Andreevab0034b2014-12-11 18:31:51 +0200190**num_fields()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500191
192The number of FIELDS (columns) returned by the query. Make sure to call
James L Parrye4a9f642014-11-24 16:20:53 -0800193the method using your query result object::
Derek Jones8ede1a22011-10-05 13:34:52 -0500194
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400195 $query = $this->db->query('SELECT * FROM my_table');
196
197 echo $query->num_fields();
Derek Jones8ede1a22011-10-05 13:34:52 -0500198
Andrey Andreevab0034b2014-12-11 18:31:51 +0200199**free_result()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500200
201It frees the memory associated with the result and deletes the result
202resource ID. Normally PHP frees its memory automatically at the end of
203script execution. However, if you are running a lot of queries in a
204particular script you might want to free the result after each query
Andrey Andreevab0034b2014-12-11 18:31:51 +0200205result has been generated in order to cut down on memory consumption.
206
Derek Jones8ede1a22011-10-05 13:34:52 -0500207Example::
208
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400209 $query = $this->db->query('SELECT title FROM my_table');
210
211 foreach ($query->result() as $row)
212 {
213 echo $row->title;
214 }
Andrey Andreevab0034b2014-12-11 18:31:51 +0200215
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400216 $query->free_result(); // The $query result object will no longer be available
217
218 $query2 = $this->db->query('SELECT name FROM some_table');
219
220 $row = $query2->row();
221 echo $row->name;
Andrey Andreevfdb75412012-03-05 16:32:17 +0200222 $query2->free_result(); // The $query2 result object will no longer be available
Andrey Andreev69edc432012-12-04 13:32:16 +0200223
James L Parrye4a9f642014-11-24 16:20:53 -0800224**data_seek()**
Andrey Andreev69edc432012-12-04 13:32:16 +0200225
226This method sets the internal pointer for the next result row to be
227fetched. It is only useful in combination with ``unbuffered_row()``.
228
229It accepts a positive integer value, which defaults to 0 and returns
230TRUE on success or FALSE on failure.
231
232::
233
234 $query = $this->db->query('SELECT `field_name` FROM `table_name`');
235 $query->data_seek(5); // Skip the first 5 rows
236 $row = $query->unbuffered_row();
237
238.. note:: Not all database drivers support this feature and will return FALSE.
James L Parry15162192014-12-05 23:40:47 -0800239 Most notably - you won't be able to use it with PDO.
240
241***************
242Class Reference
243***************
244
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200245.. php:class:: CI_DB_result
James L Parry15162192014-12-05 23:40:47 -0800246
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200247 .. php:method:: result([$type = 'object'])
Andrey Andreevab0034b2014-12-11 18:31:51 +0200248
249 :param string $type: Type of requested results - array, object, or class name
250 :returns: Array containing the fetched rows
251 :rtype: array
252
253 A wrapper for the ``result_array()``, ``result_object()``
254 and ``custom_result_object()`` methods.
255
256 Usage: see `Result Arrays`_.
257
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200258 .. php:method:: result_array()
Andrey Andreevab0034b2014-12-11 18:31:51 +0200259
260 :returns: Array containing the fetched rows
261 :rtype: array
262
263 Returns the query results as an array of rows, where each
264 row is itself an associative array.
265
266 Usage: see `Result Arrays`_.
267
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200268 .. php:method:: result_object()
Andrey Andreevab0034b2014-12-11 18:31:51 +0200269
270 :returns: Array containing the fetched rows
271 :rtype: array
272
273 Returns the query results as an array of rows, where each
274 row is an object of type ``stdClass``.
275
276 Usage: see `Result Arrays`_.
277
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200278 .. php:method:: custom_result_object($class_name)
James L Parry15162192014-12-05 23:40:47 -0800279
Andrey Andreevab0034b2014-12-11 18:31:51 +0200280 :param string $class_name: Class name for the resulting rows
281 :returns: Array containing the fetched rows
282 :rtype: array
James L Parry15162192014-12-05 23:40:47 -0800283
Andrey Andreevab0034b2014-12-11 18:31:51 +0200284 Returns the query results as an array of rows, where each
285 row is an instance of the specified class.
286
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200287 .. php:method:: row([$n = 0[, $type = 'object']])
Andrey Andreevab0034b2014-12-11 18:31:51 +0200288
289 :param int $n: Index of the query results row to be returned
290 :param string $type: Type of the requested result - array, object, or class name
291 :returns: The requested row or NULL if it doesn't exist
292 :rtype: mixed
293
294 A wrapper for the ``row_array()``, ``row_object() and
295 ``custom_row_object()`` methods.
296
297 Usage: see `Result Rows`_.
298
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200299 .. php:method:: unbuffered_row([$type = 'object'])
Andrey Andreevab0034b2014-12-11 18:31:51 +0200300
301 :param string $type: Type of the requested result - array, object, or class name
302 :returns: Next row from the result set or NULL if it doesn't exist
303 :rtype: mixed
304
305 Fetches the next result row and returns it in the
306 requested form.
307
308 Usage: see `Result Rows`_.
309
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200310 .. php:method:: row_array([$n = 0])
Andrey Andreevab0034b2014-12-11 18:31:51 +0200311
312 :param int $n: Index of the query results row to be returned
313 :returns: The requested row or NULL if it doesn't exist
314 :rtype: array
315
316 Returns the requested result row as an associative array.
317
318 Usage: see `Result Rows`_.
319
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200320 .. php:method:: row_object([$n = 0])
Andrey Andreevab0034b2014-12-11 18:31:51 +0200321
322 :param int $n: Index of the query results row to be returned
323 :returns: The requested row or NULL if it doesn't exist
324 :rtype: stdClass
325
326 Returns the requested result row as an object of type
327 ``stdClass``.
328
329 Usage: see `Result Rows`_.
James L Parry15162192014-12-05 23:40:47 -0800330
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200331 .. php:method:: custom_row_object($n, $type)
James L Parry15162192014-12-05 23:40:47 -0800332
333 :param int $n: Index of the results row to return
Andrey Andreevab0034b2014-12-11 18:31:51 +0200334 :param string $class_name: Class name for the resulting row
335 :returns: The requested row or NULL if it doesn't exist
James L Parry15162192014-12-05 23:40:47 -0800336 :rtype: $type
337
Andrey Andreevab0034b2014-12-11 18:31:51 +0200338 Returns the requested result row as an instance of the
339 requested class.
James L Parry15162192014-12-05 23:40:47 -0800340
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200341 .. php:method:: data_seek([$n = 0])
James L Parry15162192014-12-05 23:40:47 -0800342
343 :param int $n: Index of the results row to be returned next
Andrey Andreevab0034b2014-12-11 18:31:51 +0200344 :returns: TRUE on success, FALSE on failure
James L Parry15162192014-12-05 23:40:47 -0800345 :rtype: bool
346
347 Moves the internal results row pointer to the desired offset.
James L Parry15162192014-12-05 23:40:47 -0800348
Andrey Andreevab0034b2014-12-11 18:31:51 +0200349 Usage: see `Result Helper Methods`_.
James L Parry15162192014-12-05 23:40:47 -0800350
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200351 .. php:method:: set_row($key[, $value = NULL])
James L Parry15162192014-12-05 23:40:47 -0800352
Andrey Andreevab0034b2014-12-11 18:31:51 +0200353 :param mixed $key: Column name or array of key/value pairs
354 :param mixed $value: Value to assign to the column, $key is a single field name
355 :rtype: void
356
357 Assigns a value to a particular column.
358
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200359 .. php:method:: next_row([$type = 'object'])
Andrey Andreevab0034b2014-12-11 18:31:51 +0200360
361 :param string $type: Type of the requested result - array, object, or class name
362 :returns: Next row of result set, or NULL if it doesn't exist
363 :rtype: mixed
364
365 Returns the next row from the result set.
366
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200367 .. php:method:: previous_row([$type = 'object'])
Andrey Andreevab0034b2014-12-11 18:31:51 +0200368
369 :param string $type: Type of the requested result - array, object, or class name
370 :returns: Previous row of result set, or NULL if it doesn't exist
371 :rtype: mixed
372
373 Returns the previous row from the result set.
James L Parry15162192014-12-05 23:40:47 -0800374
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200375 .. php:method:: first_row([$type = 'object'])
James L Parry15162192014-12-05 23:40:47 -0800376
Andrey Andreevab0034b2014-12-11 18:31:51 +0200377 :param string $type: Type of the requested result - array, object, or class name
378 :returns: First row of result set, or NULL if it doesn't exist
James L Parry15162192014-12-05 23:40:47 -0800379 :rtype: mixed
380
Andrey Andreevab0034b2014-12-11 18:31:51 +0200381 Returns the first row from the result set.
382
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200383 .. php:method:: last_row([$type = 'object'])
Andrey Andreevab0034b2014-12-11 18:31:51 +0200384
385 :param string $type: Type of the requested result - array, object, or class name
386 :returns: Last row of result set, or NULL if it doesn't exist
387 :rtype: mixed
388
389 Returns the last row from the result set.
390
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200391 .. php:method:: num_rows()
Andrey Andreevab0034b2014-12-11 18:31:51 +0200392
393 :returns: Number of rows in the result set
394 :rtype: int
395
396 Returns the number of rows in the result set.
397
398 Usage: see `Result Helper Methods`_.
399
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200400 .. php:method:: num_fields()
Andrey Andreevab0034b2014-12-11 18:31:51 +0200401
402 :returns: Number of fields in the result set
403 :rtype: int
404
405 Returns the number of fields in the result set.
406
407 Usage: see `Result Helper Methods`_.
408
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200409 .. php:method:: field_data()
Andrey Andreevab0034b2014-12-11 18:31:51 +0200410
411 :returns: Array containing field meta-data
412 :rtype: array
413
414 Generates an array of ``stdClass`` objects containing
415 field meta-data.
James L Parry15162192014-12-05 23:40:47 -0800416
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200417 .. php:method:: free_result()
James L Parry15162192014-12-05 23:40:47 -0800418
419 :rtype: void
420
Andrey Andreevab0034b2014-12-11 18:31:51 +0200421 Frees a result set.
James L Parry15162192014-12-05 23:40:47 -0800422
Andrey Andreevab0034b2014-12-11 18:31:51 +0200423 Usage: see `Result Helper Methods`_.
James L Parry15162192014-12-05 23:40:47 -0800424
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200425 .. php:method:: list_fields()
James L Parry15162192014-12-05 23:40:47 -0800426
427 :returns: Array of column names
428 :rtype: array
429
Andrey Andreevab0034b2014-12-11 18:31:51 +0200430 Returns an array containing the field names in the
sv3tli0d829a5f2015-03-02 17:22:01 +0200431 result set.