blob: e06985130d41e91a201d027eb98261f330fb735c [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 Parry014bc892014-11-24 17:14:19 -08007*************
8Result Arrays
9*************
10
Derek Jones8ede1a22011-10-05 13:34:52 -050011result()
12========
13
James L Parrye4a9f642014-11-24 16:20:53 -080014This method returns the query result as an array of **objects**, or
Derek Jones8ede1a22011-10-05 13:34:52 -050015**an empty array** on failure. Typically you'll use this in a foreach
16loop, like this::
17
Joseph Wensleyf24f4042011-10-06 22:53:29 -040018 $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 Jones8ede1a22011-10-05 13:34:52 -050026
James L Parrye4a9f642014-11-24 16:20:53 -080027The above method is an alias of result_object().
Derek Jones8ede1a22011-10-05 13:34:52 -050028
29If you run queries that might **not** produce a result, you are
30encouraged to test the result first::
31
Joseph Wensleyf24f4042011-10-06 22:53:29 -040032 $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 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 {
53 echo $user->name; // call attributes
54 echo $user->reverse_name(); // or methods defined on the 'User' class
55 }
56
57result_array()
58===============
59
James L Parrye4a9f642014-11-24 16:20:53 -080060This method returns the query result as a pure array, or an empty
Derek Jones8ede1a22011-10-05 13:34:52 -050061array when no result is produced. Typically you'll use this in a foreach
62loop, like this::
63
Joseph Wensleyf24f4042011-10-06 22:53:29 -040064 $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 Jones8ede1a22011-10-05 13:34:52 -050072
James L Parry014bc892014-11-24 17:14:19 -080073***********
74Result Rows
75***********
76
Derek Jones8ede1a22011-10-05 13:34:52 -050077row()
78=====
79
James L Parrye4a9f642014-11-24 16:20:53 -080080This method returns a single result row. If your query has more than
Derek Jones8ede1a22011-10-05 13:34:52 -050081one row, it returns only the first row. The result is returned as an
82**object**. Here's a usage example::
83
Joseph Wensleyf24f4042011-10-06 22:53:29 -040084 $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 Jones8ede1a22011-10-05 13:34:52 -050094
95If you want a specific row returned you can submit the row number as a
96digit in the first parameter::
97
98 $row = $query->row(5);
99
100You can also add a second String parameter, which is the name of a class
101to instantiate the row with::
102
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400103 $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 Jones8ede1a22011-10-05 13:34:52 -0500108
109row_array()
Andrey Andreev69edc432012-12-04 13:32:16 +0200110===========
Derek Jones8ede1a22011-10-05 13:34:52 -0500111
James L Parrye4a9f642014-11-24 16:20:53 -0800112Identical 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
James L Parry8b855262014-11-24 17:29:04 -0800147.. note:: all the methods above will load the whole result into memory
148 (prefetching) use unbuffered_row() for processing large result sets.
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300149
Andrey Andreev69edc432012-12-04 13:32:16 +0200150unbuffered_row()
151================
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
James L Parrye4a9f642014-11-24 16:20:53 -0800179**$query->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
Timothy Warren69bb4082012-03-05 12:49:55 -0500188.. note::
Andrey Andreevfdb75412012-03-05 16:32:17 +0200189 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 Parrye4a9f642014-11-24 16:20:53 -0800192 resulting array in order to achieve the same methodality.
Andrey Andreevfdb75412012-03-05 16:32:17 +0200193
James L Parrye4a9f642014-11-24 16:20:53 -0800194**$query->num_fields()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500195
196The number of FIELDS (columns) returned by the query. Make sure to call
James L Parrye4a9f642014-11-24 16:20:53 -0800197the method using your query result object::
Derek Jones8ede1a22011-10-05 13:34:52 -0500198
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400199 $query = $this->db->query('SELECT * FROM my_table');
200
201 echo $query->num_fields();
Derek Jones8ede1a22011-10-05 13:34:52 -0500202
James L Parrye4a9f642014-11-24 16:20:53 -0800203**$query->free_result()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500204
205It frees the memory associated with the result and deletes the result
206resource ID. Normally PHP frees its memory automatically at the end of
207script execution. However, if you are running a lot of queries in a
208particular script you might want to free the result after each query
209result has been generated in order to cut down on memory consumptions.
210Example::
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 }
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 Andreevfdb75412012-03-05 16:32:17 +0200224 $query2->free_result(); // The $query2 result object will no longer be available
Andrey Andreev69edc432012-12-04 13:32:16 +0200225
James L Parrye4a9f642014-11-24 16:20:53 -0800226**data_seek()**
Andrey Andreev69edc432012-12-04 13:32:16 +0200227
228This method sets the internal pointer for the next result row to be
229fetched. It is only useful in combination with ``unbuffered_row()``.
230
231It accepts a positive integer value, which defaults to 0 and returns
232TRUE 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.