blob: 8e8d3cf968ed9d225a0f3da7c2fe5ea2b929f1f4 [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
5<title>Code Igniter User Guide</title>
6
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
10<script type="text/javascript" src="../scripts/nav.js"></script>
11<script type="text/javascript" src="../scripts/prototype.lite.js"></script>
12<script type="text/javascript" src="../scripts/moo.fx.js"></script>
13<script type="text/javascript">
14window.onload = function() {
15 myHeight = new fx.Height('nav', {duration: 400});
16 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' />
25<meta name='description' content='Code Igniter User Guide' />
26
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>
36<td><h1>Code Igniter User Guide Version 1.4.1</h1></td>
37<td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
38</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">
48<a href="http://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
49<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>
53<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.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>
54</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
74 <p>This function returns the query result as an array of <strong>objects</strong>, or <strong>FALSE</strong> on failure.
75
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>
87
88 <p>If you run queries that might <strong>not</strong> produce a result, you are encouraged to test the result first:</p>
89
90 <code>
91 $query = $this->db->query("YOUR QUERY");<br />
92 <br />
93 if ($query->num_rows() > 0)<br />
94 {<br />
95 &nbsp;&nbsp;&nbsp;foreach ($query->result() as $row)<br />
96 &nbsp;&nbsp;&nbsp;{<br />
97 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $row->title;<br />
98 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $row->name;<br />
99 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $row->body;<br />
100 &nbsp;&nbsp;&nbsp;}<br />
101 }
102 </code>
103
104 <h2>result_array()</h2>
105
106 <p>This function returns the query result as a pure array, or FALSE on failure. Typically you'll use this in a foreach loop, like this:</p>
107 <code>
108 $query = $this->db->query("YOUR QUERY");<br />
109 <br />
110 foreach ($query->result_array() as $row)<br />
111 {<br />
112 &nbsp;&nbsp;&nbsp;echo $row['title'];<br />
113 &nbsp;&nbsp;&nbsp;echo $row['name'];<br />
114 &nbsp;&nbsp;&nbsp;echo $row['body'];<br />
115 }</code>
116
117 <h2>result('array')</h2>
118
119 <p>Identical to <dfn>$this->db->result_array()</dfn>.</p>
120
121
122 <h2>row()</h2>
123
124 <p>This function returns a single result row. If your query has more than one row, it returns only the first row.
125 The result is returned as an <strong>object</strong>. Here's a usage example:</p>
126 <code>
127 $query = $this->db->query("YOUR QUERY");<br />
128 <br />
129 if ($query->num_rows() > 0)<br />
130 {<br />
131 &nbsp;&nbsp;&nbsp;$row = $query->row();
132 <br /><br />
133 &nbsp;&nbsp;&nbsp;echo $row->title;<br />
134 &nbsp;&nbsp;&nbsp;echo $row->name;<br />
135 &nbsp;&nbsp;&nbsp;echo $row->body;<br />
136 }
137 </code>
138
139 <p>If you want a specific row returned you can submit the row number as a digit in the first parameter:
140
141 <code>$row = $query->row(<dfn>5</dfn>);</code>
142
143
144 <h2>row_array()</h2>
145
146 <p>Identical to the above <var>row()</var> function, except it returns an array. Example:</p>
147
148 <code>
149 $query = $this->db->query("YOUR QUERY");<br />
150 <br />
151 if ($query->num_rows() > 0)<br />
152 {<br />
153 &nbsp;&nbsp;&nbsp;$row = $query->row_array();
154 <br /><br />
155 &nbsp;&nbsp;&nbsp;echo $row['title'];<br />
156 &nbsp;&nbsp;&nbsp;echo $row['name'];<br />
157 &nbsp;&nbsp;&nbsp;echo $row['body'];<br />
158 }
159 </code>
160
161
162 <p>If you want a specific row returned you can submit the row number as a digit in the first parameter:
163
164 <code>$row = $query->row_array(<dfn>5</dfn>);</code>
165
166
167 <p>In addition, you can walk forward/backwards/first/last through your results using these variations:</p>
168
169<p>
170 <strong>$row = $query->first_row()</strong><br />
171 <strong>$row = $query->last_row()</strong><br />
172 <strong>$row = $query->next_row()</strong><br />
173 <strong>$row = $query->previous_row()</strong>
174</p>
175
176<p>By default they return an object unless you put the word "array" in the parameter:</p>
177
178<p>
179 <strong>$row = $query->first_row('array')</strong><br />
180 <strong>$row = $query->last_row('array')</strong><br />
181 <strong>$row = $query->next_row('array')</strong><br />
182 <strong>$row = $query->previous_row('array')</strong>
183</p>
184
185
186
187<h1><br />Result Helper Functions</h1>
188
189
190<h2>$query->num_rows()</h2>
191<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>
192
193<code>$query = $this->db->query('SELECT * FROM my_table');<br /><br />
194echo $query->num_rows();
195</code>
196
197<h2>$query->num_fields()</h2>
198<p>The number of FIELDS (columns) returned by the query. Make sure to call the function using your query result object:</p>
199
200<code>$query = $this->db->query('SELECT * FROM my_table');<br /><br />
201echo $query->num_fields();
202</code>
203
204
205
206
207
208</div>
209<!-- END CONTENT -->
210
211
212<div id="footer">
213<p>
214Previous Topic:&nbsp;&nbsp;<a href="queries.html">Queries</a>
215&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
216<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
217<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
218Next Topic:&nbsp;&nbsp;<a href="helpers.html">Query Helper Functions</a>
219<p>
220<p><a href="http://www.codeigniter.com">Code Igniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 &nbsp;&middot;&nbsp; <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
221</div>
222
223</body>
224</html>