blob: c4583fb66e589e329c117775c8133cf4b9c11b94 [file] [log] [blame]
adminfb28bb82006-09-24 17:59:33 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html>
3<head>
4
Derek Allard404e35d2007-08-07 01:00:45 +00005<title>CodeIgniter User Guide : Generating Query Results</title>
adminfb28bb82006-09-24 17:59:33 +00006
7<style type='text/css' media='all'>@import url('../userguide.css');</style>
8<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
9
admin17a890d2006-09-27 20:42:42 +000010<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000011<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000012<script type="text/javascript" src="../nav/moo.fx.js"></script>
adminfb28bb82006-09-24 17:59:33 +000013<script type="text/javascript">
14window.onload = function() {
admine334c472006-10-21 19:44:22 +000015 myHeight = new fx.Height('nav', {duration: 400});
adminfb28bb82006-09-24 17:59:33 +000016 myHeight.hide();
17}
18</script>
19
20<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
21<meta http-equiv='expires' content='-1' />
22<meta http-equiv= 'pragma' content='no-cache' />
23<meta name='robots' content='all' />
24<meta name='author' content='Rick Ellis' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000025<meta name='description' content='CodeIgniter User Guide' />
adminfb28bb82006-09-24 17:59:33 +000026
27</head>
28<body>
29
30<!-- START NAVIGATION -->
31<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
32<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
33<div id="masthead">
34<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
35<tr>
Derek Allard60ca9b72007-07-12 19:53:27 +000036<td><h1>CodeIgniter User Guide Version 1.5.4</h1></td>
adminc0d5d522006-10-30 19:40:35 +000037<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminfb28bb82006-09-24 17:59:33 +000038</tr>
39</table>
40</div>
41<!-- END NAVIGATION -->
42
43
44<!-- START BREADCRUMB -->
45<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
46<tr>
47<td id="breadcrumb">
Derek Allardd2df9bc2007-04-15 17:41:17 +000048<a href="http://www.codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminfb28bb82006-09-24 17:59:33 +000049<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50<a href="index.html">Database Library</a> &nbsp;&#8250;&nbsp;
51Query Results
52</td>
Derek Allardbc030912007-06-24 18:25:29 +000053<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
adminfb28bb82006-09-24 17:59:33 +000054</tr>
55</table>
56<!-- END BREADCRUMB -->
57
58
59<br clear="all" />
60
61
62<!-- START CONTENT -->
63<div id="content">
64
65
66
67<h1>Generating Query Results</h1>
68
69
70<p>There are several ways to generate query results:</p>
71
72 <h2>result()</h2>
73
Derek Allard11b76a32007-02-02 03:46:31 +000074 <p>This function returns the query result as an array of <strong>objects</strong>, or <strong>an empty array</strong> on failure.
adminfb28bb82006-09-24 17:59:33 +000075
76 Typically you'll use this in a foreach loop, like this:</p>
77
78 <code>
79 $query = $this->db->query("YOUR QUERY");<br />
80 <br />
81 foreach ($query->result() as $row)<br />
82 {<br />
83 &nbsp;&nbsp;&nbsp;echo $row->title;<br />
84 &nbsp;&nbsp;&nbsp;echo $row->name;<br />
85 &nbsp;&nbsp;&nbsp;echo $row->body;<br />
86 }</code>
adminb2a9cec2006-10-01 03:38:04 +000087
88 <p>The above <dfn>function</dfn> is an alias of <dfn>result_object()</dfn>.</p>
adminfb28bb82006-09-24 17:59:33 +000089
90 <p>If you run queries that might <strong>not</strong> produce a result, you are encouraged to test the result first:</p>
91
92 <code>
93 $query = $this->db->query("YOUR QUERY");<br />
94 <br />
95 if ($query->num_rows() > 0)<br />
96 {<br />
97 &nbsp;&nbsp;&nbsp;foreach ($query->result() as $row)<br />
98 &nbsp;&nbsp;&nbsp;{<br />
99 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $row->title;<br />
100 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $row->name;<br />
101 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $row->body;<br />
102 &nbsp;&nbsp;&nbsp;}<br />
103 }
104 </code>
105
106 <h2>result_array()</h2>
107
Derek Allard67e10532007-02-24 15:13:17 +0000108 <p>This function returns the query result as a pure array, or an empty array when no result is produced. Typically you'll use this in a foreach loop, like this:</p>
adminfb28bb82006-09-24 17:59:33 +0000109 <code>
110 $query = $this->db->query("YOUR QUERY");<br />
111 <br />
112 foreach ($query->result_array() as $row)<br />
113 {<br />
114 &nbsp;&nbsp;&nbsp;echo $row['title'];<br />
115 &nbsp;&nbsp;&nbsp;echo $row['name'];<br />
116 &nbsp;&nbsp;&nbsp;echo $row['body'];<br />
117 }</code>
adminfb28bb82006-09-24 17:59:33 +0000118
119
120 <h2>row()</h2>
121
admine334c472006-10-21 19:44:22 +0000122 <p>This function returns a single result row. If your query has more than one row, it returns only the first row.
adminfb28bb82006-09-24 17:59:33 +0000123 The result is returned as an <strong>object</strong>. Here's a usage example:</p>
124 <code>
125 $query = $this->db->query("YOUR QUERY");<br />
126 <br />
127 if ($query->num_rows() > 0)<br />
128 {<br />
129 &nbsp;&nbsp;&nbsp;$row = $query->row();
130 <br /><br />
131 &nbsp;&nbsp;&nbsp;echo $row->title;<br />
132 &nbsp;&nbsp;&nbsp;echo $row->name;<br />
133 &nbsp;&nbsp;&nbsp;echo $row->body;<br />
134 }
135 </code>
136
Derek Allardc6441282007-07-04 23:54:32 +0000137 <p>If you want a specific row returned you can submit the row number as a digit in the first parameter:</p>
adminfb28bb82006-09-24 17:59:33 +0000138
139 <code>$row = $query->row(<dfn>5</dfn>);</code>
140
141
142 <h2>row_array()</h2>
143
144 <p>Identical to the above <var>row()</var> function, except it returns an array. Example:</p>
145
146 <code>
147 $query = $this->db->query("YOUR QUERY");<br />
148 <br />
149 if ($query->num_rows() > 0)<br />
150 {<br />
151 &nbsp;&nbsp;&nbsp;$row = $query->row_array();
152 <br /><br />
153 &nbsp;&nbsp;&nbsp;echo $row['title'];<br />
154 &nbsp;&nbsp;&nbsp;echo $row['name'];<br />
155 &nbsp;&nbsp;&nbsp;echo $row['body'];<br />
156 }
157 </code>
158
159
Derek Allardc6441282007-07-04 23:54:32 +0000160 <p>If you want a specific row returned you can submit the row number as a digit in the first parameter:</p>
adminfb28bb82006-09-24 17:59:33 +0000161
162 <code>$row = $query->row_array(<dfn>5</dfn>);</code>
163
admine334c472006-10-21 19:44:22 +0000164
adminfb28bb82006-09-24 17:59:33 +0000165 <p>In addition, you can walk forward/backwards/first/last through your results using these variations:</p>
166
167<p>
168 <strong>$row = $query->first_row()</strong><br />
169 <strong>$row = $query->last_row()</strong><br />
170 <strong>$row = $query->next_row()</strong><br />
171 <strong>$row = $query->previous_row()</strong>
172</p>
173
174<p>By default they return an object unless you put the word "array" in the parameter:</p>
175
176<p>
177 <strong>$row = $query->first_row('array')</strong><br />
178 <strong>$row = $query->last_row('array')</strong><br />
179 <strong>$row = $query->next_row('array')</strong><br />
180 <strong>$row = $query->previous_row('array')</strong>
181</p>
182
183
Derek Allardc6441282007-07-04 23:54:32 +0000184
admin78ce3cc2006-10-02 02:58:03 +0000185<h1>Result Helper Functions</h1>
adminfb28bb82006-09-24 17:59:33 +0000186
187
188<h2>$query->num_rows()</h2>
189<p>The number of rows returned by the query. Note: In this example, <dfn>$query</dfn> is the variable that the query result object is assigned to:</p>
190
191<code>$query = $this->db->query('SELECT * FROM my_table');<br /><br />
192echo $query->num_rows();
193</code>
194
195<h2>$query->num_fields()</h2>
196<p>The number of FIELDS (columns) returned by the query. Make sure to call the function using your query result object:</p>
197
198<code>$query = $this->db->query('SELECT * FROM my_table');<br /><br />
199echo $query->num_fields();
200</code>
201
202
203
adminddf83e42006-09-24 20:25:42 +0000204<h2>$query->free_result()</h2>
205<p>It frees the memory associated with the result and deletes the result resource ID. Normally PHP frees its memory automatically at the end of script
206execution. However, if you are running a lot of queries in a particular script you might want to free the result after each query result has been
207generated in order to cut down on memory consumptions. Example:
208</p>
209
210<code>$query = $this->db->query('SELECT title FROM my_table');<br /><br />
211foreach ($query->result() as $row)<br />
212{<br />
213&nbsp;&nbsp;&nbsp;echo $row->title;<br />
214}<br />
215$query->free_result(); // The $query result object will no longer be available<br />
216<br />
217$query2 = $this->db->query('SELECT name FROM some_table');<br /><br />
218$row = $query2->row();<br />
219echo $row->name;<br />
220$query2->free_result(); // The $query2 result object will no longer be available
221</code>
222
223
224
adminfb28bb82006-09-24 17:59:33 +0000225
226
227</div>
228<!-- END CONTENT -->
229
230
231<div id="footer">
232<p>
233Previous Topic:&nbsp;&nbsp;<a href="queries.html">Queries</a>
234&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
235<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
236<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
237Next Topic:&nbsp;&nbsp;<a href="helpers.html">Query Helper Functions</a>
Derek Allardc6441282007-07-04 23:54:32 +0000238</p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000239<p><a href="http://www.codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2007 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
adminfb28bb82006-09-24 17:59:33 +0000240</div>
241
242</body>
243</html>