blob: d032f734e057df8e0cf213a1fc7424e97112f748 [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()
102============
103
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
141unbuffered_row($type)
Derek Jonesce79be02012-06-25 23:23:46 -0700142=====================
Juan Ignacio Bordad981e292012-05-18 18:29:24 -0300143
144This function returns a single result row without prefetching the whole result in memory as row() does.
145If your query has more than one row, it returns the current row and moves the internal data pointer ahead.
146The result is returned as $type could be 'object' (default) or 'array' that will return an associative array.
147
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
Derek Jones8ede1a22011-10-05 13:34:52 -0500159***********************
160Result Helper Functions
161***********************
162
163$query->num_rows()
164===================
165
166The number of rows returned by the query. Note: In this example, $query
167is the variable that the query result object is assigned to::
168
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400169 $query = $this->db->query('SELECT * FROM my_table');
170
171 echo $query->num_rows();
Derek Jones8ede1a22011-10-05 13:34:52 -0500172
Timothy Warren69bb4082012-03-05 12:49:55 -0500173.. note::
Andrey Andreevfdb75412012-03-05 16:32:17 +0200174 Not all database drivers have a native way of getting the total
175 number of rows for a result set. When this is the case, all of
176 the data is prefetched and count() is manually called on the
177 resulting array in order to achieve the same functionality.
178
Derek Jones8ede1a22011-10-05 13:34:52 -0500179$query->num_fields()
180=====================
181
182The number of FIELDS (columns) returned by the query. Make sure to call
183the function using your query result object::
184
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400185 $query = $this->db->query('SELECT * FROM my_table');
186
187 echo $query->num_fields();
Derek Jones8ede1a22011-10-05 13:34:52 -0500188
189$query->free_result()
190======================
191
192It frees the memory associated with the result and deletes the result
193resource ID. Normally PHP frees its memory automatically at the end of
194script execution. However, if you are running a lot of queries in a
195particular script you might want to free the result after each query
196result has been generated in order to cut down on memory consumptions.
197Example::
198
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400199 $query = $this->db->query('SELECT title FROM my_table');
200
201 foreach ($query->result() as $row)
202 {
203 echo $row->title;
204 }
205 $query->free_result(); // The $query result object will no longer be available
206
207 $query2 = $this->db->query('SELECT name FROM some_table');
208
209 $row = $query2->row();
210 echo $row->name;
Andrey Andreevfdb75412012-03-05 16:32:17 +0200211 $query2->free_result(); // The $query2 result object will no longer be available