blob: a22c2e8c35cd908b4e22ddbfc10f8df7709f34ff [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");
36
37 if ($query->num_rows() > 0)
38 {
39 foreach ($query->result() as $row)
40 {
41 echo $row->title;
42 echo $row->name;
43 echo $row->body;
44 }
45 }
Derek Jones8ede1a22011-10-05 13:34:52 -050046
47You can also pass a string to result() which represents a class to
48instantiate for each result object (note: this class must be loaded)
49
50::
51
52 $query = $this->db->query("SELECT * FROM users;");
53
54 foreach ($query->result('User') as $user)
55 {
Andrey Andreevab0034b2014-12-11 18:31:51 +020056 echo $user->name; // access attributes
57 echo $user->reverse_name(); // or methods defined on the 'User' class
Derek Jones8ede1a22011-10-05 13:34:52 -050058 }
59
Andrey Andreevab0034b2014-12-11 18:31:51 +020060**result_array()**
Derek Jones8ede1a22011-10-05 13:34:52 -050061
James L Parrye4a9f642014-11-24 16:20:53 -080062This method returns the query result as a pure array, or an empty
Derek Jones8ede1a22011-10-05 13:34:52 -050063array when no result is produced. Typically you'll use this in a foreach
64loop, like this::
65
Joseph Wensleyf24f4042011-10-06 22:53:29 -040066 $query = $this->db->query("YOUR QUERY");
67
68 foreach ($query->result_array() as $row)
69 {
70 echo $row['title'];
71 echo $row['name'];
72 echo $row['body'];
73 }
Derek Jones8ede1a22011-10-05 13:34:52 -050074
James L Parry014bc892014-11-24 17:14:19 -080075***********
76Result Rows
77***********
78
Andrey Andreevab0034b2014-12-11 18:31:51 +020079**row()**
Derek Jones8ede1a22011-10-05 13:34:52 -050080
James L Parrye4a9f642014-11-24 16:20:53 -080081This method returns a single result row. If your query has more than
Derek Jones8ede1a22011-10-05 13:34:52 -050082one row, it returns only the first row. The result is returned as an
83**object**. Here's a usage example::
84
Joseph Wensleyf24f4042011-10-06 22:53:29 -040085 $query = $this->db->query("YOUR QUERY");
86
87 if ($query->num_rows() > 0)
88 {
89 $row = $query->row();
90
91 echo $row->title;
92 echo $row->name;
93 echo $row->body;
94 }
Derek Jones8ede1a22011-10-05 13:34:52 -050095
96If you want a specific row returned you can submit the row number as a
97digit in the first parameter::
98
99 $row = $query->row(5);
100
101You can also add a second String parameter, which is the name of a class
102to instantiate the row with::
103
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400104 $query = $this->db->query("SELECT * FROM users LIMIT 1;");
105 $query->row(0, 'User');
106
Andrey Andreevab0034b2014-12-11 18:31:51 +0200107 echo $row->name; // access attributes
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400108 echo $row->reverse_name(); // or methods defined on the 'User' class
Derek Jones8ede1a22011-10-05 13:34:52 -0500109
Andrey Andreevab0034b2014-12-11 18:31:51 +0200110**row_array()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500111
Andrey Andreevab0034b2014-12-11 18:31:51 +0200112Identical to the above ``row()`` method, except it returns an array.
Derek Jones8ede1a22011-10-05 13:34:52 -0500113Example::
114
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400115 $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 Jones8ede1a22011-10-05 13:34:52 -0500125
126If you want a specific row returned you can submit the row number as a
127digit in the first parameter::
128
129 $row = $query->row_array(5);
130
131In addition, you can walk forward/backwards/first/last through your
132results using these variations:
133
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400134 | **$row = $query->first_row()**
135 | **$row = $query->last_row()**
136 | **$row = $query->next_row()**
137 | **$row = $query->previous_row()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500138
139By default they return an object unless you put the word "array" in the
140parameter:
141
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400142 | **$row = $query->first_row('array')**
143 | **$row = $query->last_row('array')**
144 | **$row = $query->next_row('array')**
145 | **$row = $query->previous_row('array')**
Derek Jones8ede1a22011-10-05 13:34:52 -0500146
Andrey Andreevab0034b2014-12-11 18:31:51 +0200147.. note:: All the methods above will load the whole result into memory
148 (prefetching). Use ``unbuffered_row()`` for processing large
149 result sets.
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300150
Andrey Andreevab0034b2014-12-11 18:31:51 +0200151**unbuffered_row()**
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300152
Andrey Andreev69edc432012-12-04 13:32:16 +0200153This method returns a single result row without prefetching the whole
154result in memory as ``row()`` does. If your query has more than one row,
155it returns the current row and moves the internal data pointer ahead.
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300156
Derek Jonesce79be02012-06-25 23:23:46 -0700157::
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300158
159 $query = $this->db->query("YOUR QUERY");
160
Juan Ignacio Bordada7c9e02012-05-19 09:42:40 -0300161 while ($row = $query->unbuffered_row())
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300162 {
163 echo $row->title;
164 echo $row->name;
165 echo $row->body;
166 }
167
Andrey Andreev69edc432012-12-04 13:32:16 +0200168You can optionally pass 'object' (default) or 'array' in order to specify
169the 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 Parrye4a9f642014-11-24 16:20:53 -0800175*********************
176Result Helper Methods
177*********************
Derek Jones8ede1a22011-10-05 13:34:52 -0500178
Andrey Andreevab0034b2014-12-11 18:31:51 +0200179**num_rows()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500180
181The number of rows returned by the query. Note: In this example, $query
182is the variable that the query result object is assigned to::
183
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400184 $query = $this->db->query('SELECT * FROM my_table');
185
186 echo $query->num_rows();
Derek Jones8ede1a22011-10-05 13:34:52 -0500187
Andrey Andreevab0034b2014-12-11 18:31:51 +0200188.. note:: Not all database drivers have a native way of getting the total
Andrey Andreevfdb75412012-03-05 16:32:17 +0200189 number of rows for a result set. When this is the case, all of
Andrey Andreevab0034b2014-12-11 18:31:51 +0200190 the data is prefetched and ``count()`` is manually called on the
191 resulting array in order to achieve the same result.
Andrey Andreevfdb75412012-03-05 16:32:17 +0200192
Andrey Andreevab0034b2014-12-11 18:31:51 +0200193**num_fields()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500194
195The number of FIELDS (columns) returned by the query. Make sure to call
James L Parrye4a9f642014-11-24 16:20:53 -0800196the method using your query result object::
Derek Jones8ede1a22011-10-05 13:34:52 -0500197
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400198 $query = $this->db->query('SELECT * FROM my_table');
199
200 echo $query->num_fields();
Derek Jones8ede1a22011-10-05 13:34:52 -0500201
Andrey Andreevab0034b2014-12-11 18:31:51 +0200202**free_result()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500203
204It frees the memory associated with the result and deletes the result
205resource ID. Normally PHP frees its memory automatically at the end of
206script execution. However, if you are running a lot of queries in a
207particular script you might want to free the result after each query
Andrey Andreevab0034b2014-12-11 18:31:51 +0200208result has been generated in order to cut down on memory consumption.
209
Derek Jones8ede1a22011-10-05 13:34:52 -0500210Example::
211
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400212 $query = $this->db->query('SELECT title FROM my_table');
213
214 foreach ($query->result() as $row)
215 {
216 echo $row->title;
217 }
Andrey Andreevab0034b2014-12-11 18:31:51 +0200218
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400219 $query->free_result(); // The $query result object will no longer be available
220
221 $query2 = $this->db->query('SELECT name FROM some_table');
222
223 $row = $query2->row();
224 echo $row->name;
Andrey Andreevfdb75412012-03-05 16:32:17 +0200225 $query2->free_result(); // The $query2 result object will no longer be available
Andrey Andreev69edc432012-12-04 13:32:16 +0200226
James L Parrye4a9f642014-11-24 16:20:53 -0800227**data_seek()**
Andrey Andreev69edc432012-12-04 13:32:16 +0200228
229This method sets the internal pointer for the next result row to be
230fetched. It is only useful in combination with ``unbuffered_row()``.
231
232It accepts a positive integer value, which defaults to 0 and returns
233TRUE on success or FALSE on failure.
234
235::
236
237 $query = $this->db->query('SELECT `field_name` FROM `table_name`');
238 $query->data_seek(5); // Skip the first 5 rows
239 $row = $query->unbuffered_row();
240
241.. note:: Not all database drivers support this feature and will return FALSE.
James L Parry15162192014-12-05 23:40:47 -0800242 Most notably - you won't be able to use it with PDO.
243
244***************
245Class Reference
246***************
247
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200248.. php:class:: CI_DB_result
James L Parry15162192014-12-05 23:40:47 -0800249
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200250 .. php:method:: result([$type = 'object'])
Andrey Andreevab0034b2014-12-11 18:31:51 +0200251
252 :param string $type: Type of requested results - array, object, or class name
253 :returns: Array containing the fetched rows
254 :rtype: array
255
256 A wrapper for the ``result_array()``, ``result_object()``
257 and ``custom_result_object()`` methods.
258
259 Usage: see `Result Arrays`_.
260
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200261 .. php:method:: result_array()
Andrey Andreevab0034b2014-12-11 18:31:51 +0200262
263 :returns: Array containing the fetched rows
264 :rtype: array
265
266 Returns the query results as an array of rows, where each
267 row is itself an associative array.
268
269 Usage: see `Result Arrays`_.
270
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200271 .. php:method:: result_object()
Andrey Andreevab0034b2014-12-11 18:31:51 +0200272
273 :returns: Array containing the fetched rows
274 :rtype: array
275
276 Returns the query results as an array of rows, where each
277 row is an object of type ``stdClass``.
278
279 Usage: see `Result Arrays`_.
280
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200281 .. php:method:: custom_result_object($class_name)
James L Parry15162192014-12-05 23:40:47 -0800282
Andrey Andreevab0034b2014-12-11 18:31:51 +0200283 :param string $class_name: Class name for the resulting rows
284 :returns: Array containing the fetched rows
285 :rtype: array
James L Parry15162192014-12-05 23:40:47 -0800286
Andrey Andreevab0034b2014-12-11 18:31:51 +0200287 Returns the query results as an array of rows, where each
288 row is an instance of the specified class.
289
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200290 .. php:method:: row([$n = 0[, $type = 'object']])
Andrey Andreevab0034b2014-12-11 18:31:51 +0200291
292 :param int $n: Index of the query results row to be returned
293 :param string $type: Type of the requested result - array, object, or class name
294 :returns: The requested row or NULL if it doesn't exist
295 :rtype: mixed
296
297 A wrapper for the ``row_array()``, ``row_object() and
298 ``custom_row_object()`` methods.
299
300 Usage: see `Result Rows`_.
301
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200302 .. php:method:: unbuffered_row([$type = 'object'])
Andrey Andreevab0034b2014-12-11 18:31:51 +0200303
304 :param string $type: Type of the requested result - array, object, or class name
305 :returns: Next row from the result set or NULL if it doesn't exist
306 :rtype: mixed
307
308 Fetches the next result row and returns it in the
309 requested form.
310
311 Usage: see `Result Rows`_.
312
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200313 .. php:method:: row_array([$n = 0])
Andrey Andreevab0034b2014-12-11 18:31:51 +0200314
315 :param int $n: Index of the query results row to be returned
316 :returns: The requested row or NULL if it doesn't exist
317 :rtype: array
318
319 Returns the requested result row as an associative array.
320
321 Usage: see `Result Rows`_.
322
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200323 .. php:method:: row_object([$n = 0])
Andrey Andreevab0034b2014-12-11 18:31:51 +0200324
325 :param int $n: Index of the query results row to be returned
326 :returns: The requested row or NULL if it doesn't exist
327 :rtype: stdClass
328
329 Returns the requested result row as an object of type
330 ``stdClass``.
331
332 Usage: see `Result Rows`_.
James L Parry15162192014-12-05 23:40:47 -0800333
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200334 .. php:method:: custom_row_object($n, $type)
James L Parry15162192014-12-05 23:40:47 -0800335
336 :param int $n: Index of the results row to return
Andrey Andreevab0034b2014-12-11 18:31:51 +0200337 :param string $class_name: Class name for the resulting row
338 :returns: The requested row or NULL if it doesn't exist
James L Parry15162192014-12-05 23:40:47 -0800339 :rtype: $type
340
Andrey Andreevab0034b2014-12-11 18:31:51 +0200341 Returns the requested result row as an instance of the
342 requested class.
James L Parry15162192014-12-05 23:40:47 -0800343
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200344 .. php:method:: data_seek([$n = 0])
James L Parry15162192014-12-05 23:40:47 -0800345
346 :param int $n: Index of the results row to be returned next
Andrey Andreevab0034b2014-12-11 18:31:51 +0200347 :returns: TRUE on success, FALSE on failure
James L Parry15162192014-12-05 23:40:47 -0800348 :rtype: bool
349
350 Moves the internal results row pointer to the desired offset.
James L Parry15162192014-12-05 23:40:47 -0800351
Andrey Andreevab0034b2014-12-11 18:31:51 +0200352 Usage: see `Result Helper Methods`_.
James L Parry15162192014-12-05 23:40:47 -0800353
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200354 .. php:method:: set_row($key[, $value = NULL])
James L Parry15162192014-12-05 23:40:47 -0800355
Andrey Andreevab0034b2014-12-11 18:31:51 +0200356 :param mixed $key: Column name or array of key/value pairs
357 :param mixed $value: Value to assign to the column, $key is a single field name
358 :rtype: void
359
360 Assigns a value to a particular column.
361
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200362 .. php:method:: next_row([$type = 'object'])
Andrey Andreevab0034b2014-12-11 18:31:51 +0200363
364 :param string $type: Type of the requested result - array, object, or class name
365 :returns: Next row of result set, or NULL if it doesn't exist
366 :rtype: mixed
367
368 Returns the next row from the result set.
369
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200370 .. php:method:: previous_row([$type = 'object'])
Andrey Andreevab0034b2014-12-11 18:31:51 +0200371
372 :param string $type: Type of the requested result - array, object, or class name
373 :returns: Previous row of result set, or NULL if it doesn't exist
374 :rtype: mixed
375
376 Returns the previous row from the result set.
James L Parry15162192014-12-05 23:40:47 -0800377
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200378 .. php:method:: first_row([$type = 'object'])
James L Parry15162192014-12-05 23:40:47 -0800379
Andrey Andreevab0034b2014-12-11 18:31:51 +0200380 :param string $type: Type of the requested result - array, object, or class name
381 :returns: First row of result set, or NULL if it doesn't exist
James L Parry15162192014-12-05 23:40:47 -0800382 :rtype: mixed
383
Andrey Andreevab0034b2014-12-11 18:31:51 +0200384 Returns the first row from the result set.
385
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200386 .. php:method:: last_row([$type = 'object'])
Andrey Andreevab0034b2014-12-11 18:31:51 +0200387
388 :param string $type: Type of the requested result - array, object, or class name
389 :returns: Last row of result set, or NULL if it doesn't exist
390 :rtype: mixed
391
392 Returns the last row from the result set.
393
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200394 .. php:method:: num_rows()
Andrey Andreevab0034b2014-12-11 18:31:51 +0200395
396 :returns: Number of rows in the result set
397 :rtype: int
398
399 Returns the number of rows in the result set.
400
401 Usage: see `Result Helper Methods`_.
402
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200403 .. php:method:: num_fields()
Andrey Andreevab0034b2014-12-11 18:31:51 +0200404
405 :returns: Number of fields in the result set
406 :rtype: int
407
408 Returns the number of fields in the result set.
409
410 Usage: see `Result Helper Methods`_.
411
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200412 .. php:method:: field_data()
Andrey Andreevab0034b2014-12-11 18:31:51 +0200413
414 :returns: Array containing field meta-data
415 :rtype: array
416
417 Generates an array of ``stdClass`` objects containing
418 field meta-data.
James L Parry15162192014-12-05 23:40:47 -0800419
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200420 .. php:method:: free_result()
James L Parry15162192014-12-05 23:40:47 -0800421
422 :rtype: void
423
Andrey Andreevab0034b2014-12-11 18:31:51 +0200424 Frees a result set.
James L Parry15162192014-12-05 23:40:47 -0800425
Andrey Andreevab0034b2014-12-11 18:31:51 +0200426 Usage: see `Result Helper Methods`_.
James L Parry15162192014-12-05 23:40:47 -0800427
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200428 .. php:method:: list_fields()
James L Parry15162192014-12-05 23:40:47 -0800429
430 :returns: Array of column names
431 :rtype: array
432
Andrey Andreevab0034b2014-12-11 18:31:51 +0200433 Returns an array containing the field names in the
434 result set.