blob: e0a87a8515aec924c76f10bdd396ce044f199f9e [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
7result()
8========
9
10This function returns the query result as an array of **objects**, or
11**an empty array** on failure. Typically you'll use this in a foreach
12loop, like this::
13
Joseph Wensleyf24f4042011-10-06 22:53:29 -040014 $query = $this->db->query("YOUR QUERY");
15
16 foreach ($query->result() as $row)
17 {
18 echo $row->title;
19 echo $row->name;
20 echo $row->body;
21 }
Derek Jones8ede1a22011-10-05 13:34:52 -050022
23The above function is an alias of result_object().
24
25If you run queries that might **not** produce a result, you are
26encouraged to test the result first::
27
Joseph Wensleyf24f4042011-10-06 22:53:29 -040028 $query = $this->db->query("YOUR QUERY");
29
30 if ($query->num_rows() > 0)
31 {
32 foreach ($query->result() as $row)
33 {
34 echo $row->title;
35 echo $row->name;
36 echo $row->body;
37 }
38 }
Derek Jones8ede1a22011-10-05 13:34:52 -050039
40You can also pass a string to result() which represents a class to
41instantiate for each result object (note: this class must be loaded)
42
43::
44
45 $query = $this->db->query("SELECT * FROM users;");
46
47 foreach ($query->result('User') as $user)
48 {
49 echo $user->name; // call attributes
50 echo $user->reverse_name(); // or methods defined on the 'User' class
51 }
52
53result_array()
54===============
55
56This function returns the query result as a pure array, or an empty
57array when no result is produced. Typically you'll use this in a foreach
58loop, like this::
59
Joseph Wensleyf24f4042011-10-06 22:53:29 -040060 $query = $this->db->query("YOUR QUERY");
61
62 foreach ($query->result_array() as $row)
63 {
64 echo $row['title'];
65 echo $row['name'];
66 echo $row['body'];
67 }
Derek Jones8ede1a22011-10-05 13:34:52 -050068
69row()
70=====
71
72This function returns a single result row. If your query has more than
73one row, it returns only the first row. The result is returned as an
74**object**. Here's a usage example::
75
Joseph Wensleyf24f4042011-10-06 22:53:29 -040076 $query = $this->db->query("YOUR QUERY");
77
78 if ($query->num_rows() > 0)
79 {
80 $row = $query->row();
81
82 echo $row->title;
83 echo $row->name;
84 echo $row->body;
85 }
Derek Jones8ede1a22011-10-05 13:34:52 -050086
87If you want a specific row returned you can submit the row number as a
88digit in the first parameter::
89
90 $row = $query->row(5);
91
92You can also add a second String parameter, which is the name of a class
93to instantiate the row with::
94
Joseph Wensleyf24f4042011-10-06 22:53:29 -040095 $query = $this->db->query("SELECT * FROM users LIMIT 1;");
96 $query->row(0, 'User');
97
98 echo $row->name; // call attributes
99 echo $row->reverse_name(); // or methods defined on the 'User' class
Derek Jones8ede1a22011-10-05 13:34:52 -0500100
101row_array()
Andrey Andreev69edc432012-12-04 13:32:16 +0200102===========
Derek Jones8ede1a22011-10-05 13:34:52 -0500103
104Identical to the above row() function, except it returns an array.
105Example::
106
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400107 $query = $this->db->query("YOUR QUERY");
108
109 if ($query->num_rows() > 0)
110 {
111 $row = $query->row_array();
112
113 echo $row['title'];
114 echo $row['name'];
115 echo $row['body'];
116 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500117
118If you want a specific row returned you can submit the row number as a
119digit in the first parameter::
120
121 $row = $query->row_array(5);
122
123In addition, you can walk forward/backwards/first/last through your
124results using these variations:
125
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400126 | **$row = $query->first_row()**
127 | **$row = $query->last_row()**
128 | **$row = $query->next_row()**
129 | **$row = $query->previous_row()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
131By default they return an object unless you put the word "array" in the
132parameter:
133
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400134 | **$row = $query->first_row('array')**
135 | **$row = $query->last_row('array')**
136 | **$row = $query->next_row('array')**
137 | **$row = $query->previous_row('array')**
Derek Jones8ede1a22011-10-05 13:34:52 -0500138
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300139.. note:: all the functions above will load the whole result into memory (prefetching) use unbuffered_row() for processing large result sets.
140
Andrey Andreev69edc432012-12-04 13:32:16 +0200141unbuffered_row()
142================
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300143
Andrey Andreev69edc432012-12-04 13:32:16 +0200144This method returns a single result row without prefetching the whole
145result in memory as ``row()`` does. If your query has more than one row,
146it returns the current row and moves the internal data pointer ahead.
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300147
Derek Jonesce79be02012-06-25 23:23:46 -0700148::
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300149
150 $query = $this->db->query("YOUR QUERY");
151
Juan Ignacio Bordada7c9e02012-05-19 09:42:40 -0300152 while ($row = $query->unbuffered_row())
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300153 {
154 echo $row->title;
155 echo $row->name;
156 echo $row->body;
157 }
158
Andrey Andreev69edc432012-12-04 13:32:16 +0200159You can optionally pass 'object' (default) or 'array' in order to specify
160the returned value's type::
161
162 $query->unbuffered_row(); // object
163 $query->unbuffered_row('object'); // object
164 $query->unbuffered_row('array'); // associative array
165
Derek Jones8ede1a22011-10-05 13:34:52 -0500166***********************
167Result Helper Functions
168***********************
169
170$query->num_rows()
Andrey Andreev69edc432012-12-04 13:32:16 +0200171==================
Derek Jones8ede1a22011-10-05 13:34:52 -0500172
173The number of rows returned by the query. Note: In this example, $query
174is the variable that the query result object is assigned to::
175
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400176 $query = $this->db->query('SELECT * FROM my_table');
177
178 echo $query->num_rows();
Derek Jones8ede1a22011-10-05 13:34:52 -0500179
Timothy Warren69bb4082012-03-05 12:49:55 -0500180.. note::
Andrey Andreevfdb75412012-03-05 16:32:17 +0200181 Not all database drivers have a native way of getting the total
182 number of rows for a result set. When this is the case, all of
183 the data is prefetched and count() is manually called on the
184 resulting array in order to achieve the same functionality.
185
Derek Jones8ede1a22011-10-05 13:34:52 -0500186$query->num_fields()
Andrey Andreev69edc432012-12-04 13:32:16 +0200187====================
Derek Jones8ede1a22011-10-05 13:34:52 -0500188
189The number of FIELDS (columns) returned by the query. Make sure to call
190the function using your query result object::
191
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400192 $query = $this->db->query('SELECT * FROM my_table');
193
194 echo $query->num_fields();
Derek Jones8ede1a22011-10-05 13:34:52 -0500195
196$query->free_result()
Andrey Andreev69edc432012-12-04 13:32:16 +0200197=====================
Derek Jones8ede1a22011-10-05 13:34:52 -0500198
199It frees the memory associated with the result and deletes the result
200resource ID. Normally PHP frees its memory automatically at the end of
201script execution. However, if you are running a lot of queries in a
202particular script you might want to free the result after each query
203result has been generated in order to cut down on memory consumptions.
204Example::
205
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400206 $query = $this->db->query('SELECT title FROM my_table');
207
208 foreach ($query->result() as $row)
209 {
210 echo $row->title;
211 }
212 $query->free_result(); // The $query result object will no longer be available
213
214 $query2 = $this->db->query('SELECT name FROM some_table');
215
216 $row = $query2->row();
217 echo $row->name;
Andrey Andreevfdb75412012-03-05 16:32:17 +0200218 $query2->free_result(); // The $query2 result object will no longer be available
Andrey Andreev69edc432012-12-04 13:32:16 +0200219
220data_seek()
221===========
222
223This method sets the internal pointer for the next result row to be
224fetched. It is only useful in combination with ``unbuffered_row()``.
225
226It accepts a positive integer value, which defaults to 0 and returns
227TRUE on success or FALSE on failure.
228
229::
230
231 $query = $this->db->query('SELECT `field_name` FROM `table_name`');
232 $query->data_seek(5); // Skip the first 5 rows
233 $row = $query->unbuffered_row();
234
235.. note:: Not all database drivers support this feature and will return FALSE.
236 Most notably - you won't be able to use it with PDO.