blob: 865345762f43d5b47f15c8a725256aa183e21fcd [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
139***********************
140Result Helper Functions
141***********************
142
143$query->num_rows()
144===================
145
146The number of rows returned by the query. Note: In this example, $query
147is the variable that the query result object is assigned to::
148
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400149 $query = $this->db->query('SELECT * FROM my_table');
150
151 echo $query->num_rows();
Derek Jones8ede1a22011-10-05 13:34:52 -0500152
Timothy Warren69bb4082012-03-05 12:49:55 -0500153.. note::
Andrey Andreevfdb75412012-03-05 16:32:17 +0200154 Not all database drivers have a native way of getting the total
155 number of rows for a result set. When this is the case, all of
156 the data is prefetched and count() is manually called on the
157 resulting array in order to achieve the same functionality.
158
Derek Jones8ede1a22011-10-05 13:34:52 -0500159$query->num_fields()
160=====================
161
162The number of FIELDS (columns) returned by the query. Make sure to call
163the function using your query result object::
164
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400165 $query = $this->db->query('SELECT * FROM my_table');
166
167 echo $query->num_fields();
Derek Jones8ede1a22011-10-05 13:34:52 -0500168
169$query->free_result()
170======================
171
172It frees the memory associated with the result and deletes the result
173resource ID. Normally PHP frees its memory automatically at the end of
174script execution. However, if you are running a lot of queries in a
175particular script you might want to free the result after each query
176result has been generated in order to cut down on memory consumptions.
177Example::
178
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400179 $query = $this->db->query('SELECT title FROM my_table');
180
181 foreach ($query->result() as $row)
182 {
183 echo $row->title;
184 }
185 $query->free_result(); // The $query result object will no longer be available
186
187 $query2 = $this->db->query('SELECT name FROM some_table');
188
189 $row = $query2->row();
190 echo $row->name;
Andrey Andreevfdb75412012-03-05 16:32:17 +0200191 $query2->free_result(); // The $query2 result object will no longer be available