blob: 044360ebbd84a49f4f70e8206f7b369d01a3f049 [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
Derek Jones8ede1a22011-10-05 13:34:52 -050015result()
16========
17
James L Parrye4a9f642014-11-24 16:20:53 -080018This method returns the query result as an array of **objects**, or
Derek Jones8ede1a22011-10-05 13:34:52 -050019**an empty array** on failure. Typically you'll use this in a foreach
20loop, like this::
21
Joseph Wensleyf24f4042011-10-06 22:53:29 -040022 $query = $this->db->query("YOUR QUERY");
23
24 foreach ($query->result() as $row)
25 {
26 echo $row->title;
27 echo $row->name;
28 echo $row->body;
29 }
Derek Jones8ede1a22011-10-05 13:34:52 -050030
James L Parrye4a9f642014-11-24 16:20:53 -080031The above method is an alias of result_object().
Derek Jones8ede1a22011-10-05 13:34:52 -050032
33If you run queries that might **not** produce a result, you are
34encouraged to test the result first::
35
Joseph Wensleyf24f4042011-10-06 22:53:29 -040036 $query = $this->db->query("YOUR QUERY");
37
38 if ($query->num_rows() > 0)
39 {
40 foreach ($query->result() as $row)
41 {
42 echo $row->title;
43 echo $row->name;
44 echo $row->body;
45 }
46 }
Derek Jones8ede1a22011-10-05 13:34:52 -050047
48You can also pass a string to result() which represents a class to
49instantiate for each result object (note: this class must be loaded)
50
51::
52
53 $query = $this->db->query("SELECT * FROM users;");
54
55 foreach ($query->result('User') as $user)
56 {
57 echo $user->name; // call attributes
58 echo $user->reverse_name(); // or methods defined on the 'User' class
59 }
60
61result_array()
62===============
63
James L Parrye4a9f642014-11-24 16:20:53 -080064This method returns the query result as a pure array, or an empty
Derek Jones8ede1a22011-10-05 13:34:52 -050065array when no result is produced. Typically you'll use this in a foreach
66loop, like this::
67
Joseph Wensleyf24f4042011-10-06 22:53:29 -040068 $query = $this->db->query("YOUR QUERY");
69
70 foreach ($query->result_array() as $row)
71 {
72 echo $row['title'];
73 echo $row['name'];
74 echo $row['body'];
75 }
Derek Jones8ede1a22011-10-05 13:34:52 -050076
James L Parry014bc892014-11-24 17:14:19 -080077***********
78Result Rows
79***********
80
Derek Jones8ede1a22011-10-05 13:34:52 -050081row()
82=====
83
James L Parrye4a9f642014-11-24 16:20:53 -080084This method returns a single result row. If your query has more than
Derek Jones8ede1a22011-10-05 13:34:52 -050085one row, it returns only the first row. The result is returned as an
86**object**. Here's a usage example::
87
Joseph Wensleyf24f4042011-10-06 22:53:29 -040088 $query = $this->db->query("YOUR QUERY");
89
90 if ($query->num_rows() > 0)
91 {
92 $row = $query->row();
93
94 echo $row->title;
95 echo $row->name;
96 echo $row->body;
97 }
Derek Jones8ede1a22011-10-05 13:34:52 -050098
99If you want a specific row returned you can submit the row number as a
100digit in the first parameter::
101
102 $row = $query->row(5);
103
104You can also add a second String parameter, which is the name of a class
105to instantiate the row with::
106
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400107 $query = $this->db->query("SELECT * FROM users LIMIT 1;");
108 $query->row(0, 'User');
109
110 echo $row->name; // call attributes
111 echo $row->reverse_name(); // or methods defined on the 'User' class
Derek Jones8ede1a22011-10-05 13:34:52 -0500112
113row_array()
Andrey Andreev69edc432012-12-04 13:32:16 +0200114===========
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
James L Parrye4a9f642014-11-24 16:20:53 -0800116Identical to the above row() method, except it returns an array.
Derek Jones8ede1a22011-10-05 13:34:52 -0500117Example::
118
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400119 $query = $this->db->query("YOUR QUERY");
120
121 if ($query->num_rows() > 0)
122 {
123 $row = $query->row_array();
124
125 echo $row['title'];
126 echo $row['name'];
127 echo $row['body'];
128 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
130If you want a specific row returned you can submit the row number as a
131digit in the first parameter::
132
133 $row = $query->row_array(5);
134
135In addition, you can walk forward/backwards/first/last through your
136results using these variations:
137
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400138 | **$row = $query->first_row()**
139 | **$row = $query->last_row()**
140 | **$row = $query->next_row()**
141 | **$row = $query->previous_row()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500142
143By default they return an object unless you put the word "array" in the
144parameter:
145
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400146 | **$row = $query->first_row('array')**
147 | **$row = $query->last_row('array')**
148 | **$row = $query->next_row('array')**
149 | **$row = $query->previous_row('array')**
Derek Jones8ede1a22011-10-05 13:34:52 -0500150
James L Parry8b855262014-11-24 17:29:04 -0800151.. note:: all the methods above will load the whole result into memory
152 (prefetching) use unbuffered_row() for processing large result sets.
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300153
Andrey Andreev69edc432012-12-04 13:32:16 +0200154unbuffered_row()
155================
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300156
Andrey Andreev69edc432012-12-04 13:32:16 +0200157This method returns a single result row without prefetching the whole
158result in memory as ``row()`` does. If your query has more than one row,
159it returns the current row and moves the internal data pointer ahead.
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300160
Derek Jonesce79be02012-06-25 23:23:46 -0700161::
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300162
163 $query = $this->db->query("YOUR QUERY");
164
Juan Ignacio Bordada7c9e02012-05-19 09:42:40 -0300165 while ($row = $query->unbuffered_row())
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300166 {
167 echo $row->title;
168 echo $row->name;
169 echo $row->body;
170 }
171
Andrey Andreev69edc432012-12-04 13:32:16 +0200172You can optionally pass 'object' (default) or 'array' in order to specify
173the returned value's type::
174
175 $query->unbuffered_row(); // object
176 $query->unbuffered_row('object'); // object
177 $query->unbuffered_row('array'); // associative array
178
James L Parrye4a9f642014-11-24 16:20:53 -0800179*********************
180Result Helper Methods
181*********************
Derek Jones8ede1a22011-10-05 13:34:52 -0500182
James L Parrye4a9f642014-11-24 16:20:53 -0800183**$query->num_rows()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500184
185The number of rows returned by the query. Note: In this example, $query
186is the variable that the query result object is assigned to::
187
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400188 $query = $this->db->query('SELECT * FROM my_table');
189
190 echo $query->num_rows();
Derek Jones8ede1a22011-10-05 13:34:52 -0500191
Timothy Warren69bb4082012-03-05 12:49:55 -0500192.. note::
Andrey Andreevfdb75412012-03-05 16:32:17 +0200193 Not all database drivers have a native way of getting the total
194 number of rows for a result set. When this is the case, all of
195 the data is prefetched and count() is manually called on the
James L Parrye4a9f642014-11-24 16:20:53 -0800196 resulting array in order to achieve the same methodality.
Andrey Andreevfdb75412012-03-05 16:32:17 +0200197
James L Parrye4a9f642014-11-24 16:20:53 -0800198**$query->num_fields()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500199
200The number of FIELDS (columns) returned by the query. Make sure to call
James L Parrye4a9f642014-11-24 16:20:53 -0800201the method using your query result object::
Derek Jones8ede1a22011-10-05 13:34:52 -0500202
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400203 $query = $this->db->query('SELECT * FROM my_table');
204
205 echo $query->num_fields();
Derek Jones8ede1a22011-10-05 13:34:52 -0500206
James L Parrye4a9f642014-11-24 16:20:53 -0800207**$query->free_result()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500208
209It frees the memory associated with the result and deletes the result
210resource ID. Normally PHP frees its memory automatically at the end of
211script execution. However, if you are running a lot of queries in a
212particular script you might want to free the result after each query
213result has been generated in order to cut down on memory consumptions.
214Example::
215
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400216 $query = $this->db->query('SELECT title FROM my_table');
217
218 foreach ($query->result() as $row)
219 {
220 echo $row->title;
221 }
222 $query->free_result(); // The $query result object will no longer be available
223
224 $query2 = $this->db->query('SELECT name FROM some_table');
225
226 $row = $query2->row();
227 echo $row->name;
Andrey Andreevfdb75412012-03-05 16:32:17 +0200228 $query2->free_result(); // The $query2 result object will no longer be available
Andrey Andreev69edc432012-12-04 13:32:16 +0200229
James L Parrye4a9f642014-11-24 16:20:53 -0800230**data_seek()**
Andrey Andreev69edc432012-12-04 13:32:16 +0200231
232This method sets the internal pointer for the next result row to be
233fetched. It is only useful in combination with ``unbuffered_row()``.
234
235It accepts a positive integer value, which defaults to 0 and returns
236TRUE on success or FALSE on failure.
237
238::
239
240 $query = $this->db->query('SELECT `field_name` FROM `table_name`');
241 $query->data_seek(5); // Skip the first 5 rows
242 $row = $query->unbuffered_row();
243
244.. note:: Not all database drivers support this feature and will return FALSE.
James L Parry15162192014-12-05 23:40:47 -0800245 Most notably - you won't be able to use it with PDO.
246
247***************
248Class Reference
249***************
250
251.. class:: CI_DB_result
252
253 .. method:: custom_result_object($class_name)
254
255 :param string $class_name: Class name for the results
256 :returns: Array of objects of type $class_name
257 :rtype: array of $class_name
258
259 Return the query results as an array of the specified class.
260
261 .. method:: custom_row_object($n, $type)
262
263 :param int $n: Index of the results row to return
264 :param string $class_name: Class name for the results
265 :returns: Object of type $type
266 :rtype: $type
267
268 Return a specific row from the query results as an object of
269 the specified class.
270
James L Parry8b875ce2014-12-08 03:00:31 -0800271 .. method:: data_seek([$n = 0])
James L Parry15162192014-12-05 23:40:47 -0800272
273 :param int $n: Index of the results row to be returned next
274 :returns: TRUE on success, FALSE otherwise
275 :rtype: bool
276
277 Moves the internal results row pointer to the desired offset.
278 Usage: see `Result Helper Methods`_.
279
280 .. method:: field_data()
281
282 :returns: Array of objects containing field meta-data.
283 :rtype: array
284
285 Generates an array of objects containing field meta-data.
286
James L Parry8b875ce2014-12-08 03:00:31 -0800287 .. method:: first_row([$type = 'object'])
James L Parry15162192014-12-05 23:40:47 -0800288
289 :param string $type: Type of result requested - array, object, or class name
290 :returns: First row of result set
291 :rtype: mixed
292
293 Returns the "first" row, as an array, generic object, or
294 object of a specific class
295
296 .. method:: free_result()
297
298 :rtype: void
299
300 Free the result.
301 Usage: see `Result Helper Methods`_.
302
James L Parry8b875ce2014-12-08 03:00:31 -0800303 .. method:: last_row([$type = 'object'])
James L Parry15162192014-12-05 23:40:47 -0800304
305 :param string $type: Type of result requested - array, object, or class name
306 :returns: Last row of result set
307 :rtype: mixed
308
309 Returns the "last" row, as an array, generic object, or
310 object of a specific class
311
312 .. method:: list_fields()
313
314 :returns: Array of column names
315 :rtype: array
316
317 Fetch Field Names
318
James L Parry8b875ce2014-12-08 03:00:31 -0800319 .. method:: next_row([$type = 'object'])
James L Parry15162192014-12-05 23:40:47 -0800320
321 :param string $type: Type of result requested - array, object, or class name
322 :returns: "Next" row of result set, NULL if there isn't one
323 :rtype: mixed
324
325 Returns the "next" row, as an array, generic object, or
326 object of a specific class
327
328 .. method:: num_fields()
329
330 :returns: Number of fields in the result set
331 :rtype: integer
332
333 Number of fields in the result set.
334 Usage: see `Result Helper Methods`_.
335
336 .. method:: num_rows()
337
338 :returns: Number of rows in the result set
339 :rtype: integer
340
341 Number of rows in the result set.
342 Usage: see `Result Helper Methods`_.
343
James L Parry8b875ce2014-12-08 03:00:31 -0800344 .. method:: previous_row([$type = 'object'])
James L Parry15162192014-12-05 23:40:47 -0800345
346 :param string $type: Type of result requested - array, object, or class name
347 :returns: "Previous" row of result set, NULL if there isn't one
348 :rtype: mixed
349
350 Returns the "previous" row, as an array, generic object, or
351 object of a specific class
352
James L Parry8b875ce2014-12-08 03:00:31 -0800353 .. method:: result([$type = 'object'])
James L Parry15162192014-12-05 23:40:47 -0800354
355 :param string $type: Type of result requested - array, object, or class name
356 :returns: Query results as the specified type
357 :rtype: mixed
358
359 Query result. Acts as a wrapper function for the result_array,
360 result_object and custom_result_object methods.
361 Usage: see `Result Arrays`_.
362
363 .. method:: result_array()
364
365 :returns: Query results as an associative array
366 :rtype: array
367
368 Returns the query results as an array of rows, where each
369 row is itself an associative array.
370 Usage: see `Result Arrays`_.
371
372 .. method:: result_object()
373
374 :returns: Query results as an array of objects
375 :rtype: array
376
377 Returns the query results as an array of rows, where each
378 row is an object
379
James L Parry8b875ce2014-12-08 03:00:31 -0800380 .. method:: row([$n = 0[, $type = 'object']])
James L Parry15162192014-12-05 23:40:47 -0800381
382 :param integer $n: Index of the query results row to be returned
383 :param string $type: Type of result requested - array, object, or class name
384 :returns: Requested row of result set
385 :rtype: mixed
386
387 Wrapper for result_row_array, result_row_object, and
388 custom_row_object.
389 Usage: see `Result Rows`_.
390
James L Parry8b875ce2014-12-08 03:00:31 -0800391 .. method:: row_array([$n = 0])
James L Parry15162192014-12-05 23:40:47 -0800392
393 :param integer $n: Index of the query results row to be returned
394 :returns: Requested row of result set
395 :rtype: array
396
397 Returns requested result row as an associative array.
398 Usage: see `Result Rows`_.
399
James L Parry8b875ce2014-12-08 03:00:31 -0800400 .. method:: row_object([$n = 0])
James L Parry15162192014-12-05 23:40:47 -0800401
402 :param integer $n: Index of the query results row to be returned
403 :returns: Requested row of result set
404 :rtype: object
405
406 Returns requested result row as an object
407
James L Parry8b875ce2014-12-08 03:00:31 -0800408 .. method:: set_row($key[, $value = NULL])
James L Parry15162192014-12-05 23:40:47 -0800409
410 :param mixed $key: Column index or array of key/value pairs
411 :param mixed $value: Result to assign to a column if the key is an index
412 :rtype: void
413
414 Assigns an item into a particular column slot
415
James L Parry8b875ce2014-12-08 03:00:31 -0800416 .. method:: unbuffered_row([$type = 'object'])
James L Parry15162192014-12-05 23:40:47 -0800417
418 :param string $type: Type of result requested - array, object, or class name
419 :returns: Requested row of result set
420 :rtype: mixed
421
422 Fetches the next result row and returns it in the requested
423 form.
424 Usage: see `Result Rows`_.