/lenasys/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/lenasys/trunk

« back to all changes in this revision

Viewing changes to codeigniter/user_guide/database/results.html

  • Committer: galaxyAbstractor
  • Date: 2013-04-11 13:59:06 UTC
  • mto: (23.2.1 lenasys)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: galaxyabstractor@gmail.com-20130411135906-tohvazsxmw77otid
Removed codeigniter user guide, shouldn't be in the repo
Added and implemented CKEditor Wysiwyg editor for editing of pages
Made already uploaded code files visible as you edit a page
Implemented Adams dropdown menu in codeviewer

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3
 
<head>
4
 
 
5
 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
 
<title>Generating Query Results : CodeIgniter User Guide</title>
7
 
 
8
 
<style type='text/css' media='all'>@import url('../userguide.css');</style>
9
 
<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
10
 
 
11
 
<script type="text/javascript" src="../nav/nav.js"></script>
12
 
<script type="text/javascript" src="../nav/prototype.lite.js"></script>
13
 
<script type="text/javascript" src="../nav/moo.fx.js"></script>
14
 
<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
15
 
 
16
 
<meta http-equiv='expires' content='-1' />
17
 
<meta http-equiv= 'pragma' content='no-cache' />
18
 
<meta name='robots' content='all' />
19
 
<meta name='author' content='ExpressionEngine Dev Team' />
20
 
<meta name='description' content='CodeIgniter User Guide' />
21
 
 
22
 
</head>
23
 
<body>
24
 
 
25
 
<!-- START NAVIGATION -->
26
 
<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
27
 
<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
28
 
<div id="masthead">
29
 
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
30
 
<tr>
31
 
<td><h1>CodeIgniter User Guide Version 2.1.3</h1></td>
32
 
<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
33
 
</tr>
34
 
</table>
35
 
</div>
36
 
<!-- END NAVIGATION -->
37
 
 
38
 
 
39
 
<!-- START BREADCRUMB -->
40
 
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
41
 
<tr>
42
 
<td id="breadcrumb">
43
 
<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
44
 
<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45
 
<a href="index.html">Database Library</a> &nbsp;&#8250;&nbsp;
46
 
Query Results
47
 
</td>
48
 
<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>
49
 
</tr>
50
 
</table>
51
 
<!-- END BREADCRUMB -->
52
 
 
53
 
 
54
 
<br clear="all" />
55
 
 
56
 
 
57
 
<!-- START CONTENT -->
58
 
<div id="content">
59
 
 
60
 
 
61
 
 
62
 
<h1>Generating Query Results</h1>
63
 
 
64
 
 
65
 
<p>There are several ways to generate query results:</p>
66
 
 
67
 
        <h2>result()</h2>
68
 
 
69
 
        <p>This function returns the query result as an array of <strong>objects</strong>, or <strong>an empty array</strong> on failure.
70
 
 
71
 
        Typically you'll use this in a foreach loop, like this:</p>
72
 
 
73
 
        <code>
74
 
        $query = $this->db->query("YOUR QUERY");<br />
75
 
        <br />
76
 
        foreach ($query->result() as $row)<br />
77
 
        {<br />
78
 
        &nbsp;&nbsp;&nbsp;echo $row->title;<br />
79
 
        &nbsp;&nbsp;&nbsp;echo $row->name;<br />
80
 
        &nbsp;&nbsp;&nbsp;echo $row->body;<br />
81
 
        }</code>
82
 
 
83
 
        <p>The above <dfn>function</dfn> is an alias of <dfn>result_object()</dfn>.</p>
84
 
 
85
 
        <p>If you run queries that might <strong>not</strong> produce a result, you are encouraged to test the result first:</p>
86
 
 
87
 
        <code>
88
 
        $query = $this->db->query("YOUR QUERY");<br />
89
 
        <br />
90
 
        if ($query->num_rows() > 0)<br />
91
 
        {<br />
92
 
        &nbsp;&nbsp;&nbsp;foreach ($query->result() as $row)<br />
93
 
        &nbsp;&nbsp;&nbsp;{<br />
94
 
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $row->title;<br />
95
 
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $row->name;<br />
96
 
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $row->body;<br />
97
 
        &nbsp;&nbsp;&nbsp;}<br />
98
 
        }
99
 
        </code>
100
 
 
101
 
        <p>You can also pass a string to result() which represents a class to instantiate for each result object (note: this class must be loaded)</p>
102
 
 
103
 
        <code>
104
 
        $query = $this->db->query("SELECT * FROM users;");<br />
105
 
        <br />
106
 
        foreach ($query->result('User') as $row)<br />
107
 
        {<br />
108
 
        &nbsp;&nbsp;&nbsp;echo $row->name; // call attributes<br />
109
 
        &nbsp;&nbsp;&nbsp;echo $row->reverse_name(); // or methods defined on the 'User' class<br />
110
 
        }
111
 
        </code>
112
 
 
113
 
        <h2>result_array()</h2>
114
 
 
115
 
        <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>
116
 
        <code>
117
 
        $query = $this->db->query("YOUR QUERY");<br />
118
 
        <br />
119
 
        foreach ($query->result_array() as $row)<br />
120
 
        {<br />
121
 
        &nbsp;&nbsp;&nbsp;echo $row['title'];<br />
122
 
        &nbsp;&nbsp;&nbsp;echo $row['name'];<br />
123
 
        &nbsp;&nbsp;&nbsp;echo $row['body'];<br />
124
 
        }</code>
125
 
 
126
 
 
127
 
        <h2>row()</h2>
128
 
 
129
 
        <p>This function returns a single result row.  If your query has more than one row, it returns only the first row.
130
 
        The result is returned as an <strong>object</strong>.  Here's a usage example:</p>
131
 
        <code>
132
 
        $query = $this->db->query("YOUR QUERY");<br />
133
 
        <br />
134
 
        if ($query->num_rows() > 0)<br />
135
 
        {<br />
136
 
        &nbsp;&nbsp;&nbsp;$row = $query->row();
137
 
        <br /><br />
138
 
        &nbsp;&nbsp;&nbsp;echo $row->title;<br />
139
 
        &nbsp;&nbsp;&nbsp;echo $row->name;<br />
140
 
        &nbsp;&nbsp;&nbsp;echo $row->body;<br />
141
 
        }
142
 
        </code>
143
 
 
144
 
        <p>If you want a specific row returned you can submit the row number as a digit in the first parameter:</p>
145
 
 
146
 
        <code>$row = $query->row(<dfn>5</dfn>);</code>
147
 
 
148
 
        <p>You can also add a second String parameter, which is the name of a class to instantiate the row with:</p>
149
 
 
150
 
        <code>
151
 
        $query = $this->db->query("SELECT * FROM users LIMIT 1;");<br />
152
 
        <br />
153
 
        $query->row(0, 'User')<br />
154
 
        echo $row->name; // call attributes<br />
155
 
        echo $row->reverse_name(); // or methods defined on the 'User' class<br />
156
 
        </code>
157
 
 
158
 
        <h2>row_array()</h2>
159
 
 
160
 
        <p>Identical to the above <var>row()</var> function, except it returns an array.  Example:</p>
161
 
 
162
 
        <code>
163
 
        $query = $this->db->query("YOUR QUERY");<br />
164
 
        <br />
165
 
        if ($query->num_rows() > 0)<br />
166
 
        {<br />
167
 
        &nbsp;&nbsp;&nbsp;$row = $query->row_array();
168
 
        <br /><br />
169
 
        &nbsp;&nbsp;&nbsp;echo $row['title'];<br />
170
 
        &nbsp;&nbsp;&nbsp;echo $row['name'];<br />
171
 
        &nbsp;&nbsp;&nbsp;echo $row['body'];<br />
172
 
        }
173
 
        </code>
174
 
 
175
 
 
176
 
        <p>If you want a specific row returned you can submit the row number as a digit in the first parameter:</p>
177
 
 
178
 
        <code>$row = $query->row_array(<dfn>5</dfn>);</code>
179
 
 
180
 
 
181
 
        <p>In addition, you can walk forward/backwards/first/last through your results using these variations:</p>
182
 
 
183
 
<p>
184
 
        <strong>$row = $query->first_row()</strong><br />
185
 
        <strong>$row = $query->last_row()</strong><br />
186
 
        <strong>$row = $query->next_row()</strong><br />
187
 
        <strong>$row = $query->previous_row()</strong>
188
 
</p>
189
 
 
190
 
<p>By default they return an object unless you put the word "array" in the parameter:</p>
191
 
 
192
 
<p>
193
 
        <strong>$row = $query->first_row('array')</strong><br />
194
 
        <strong>$row = $query->last_row('array')</strong><br />
195
 
        <strong>$row = $query->next_row('array')</strong><br />
196
 
        <strong>$row = $query->previous_row('array')</strong>
197
 
</p>
198
 
 
199
 
 
200
 
 
201
 
<h1>Result Helper Functions</h1>
202
 
 
203
 
 
204
 
<h2>$query->num_rows()</h2>
205
 
<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>
206
 
 
207
 
<code>$query = $this->db->query('SELECT * FROM my_table');<br /><br />
208
 
echo $query->num_rows();
209
 
</code>
210
 
 
211
 
<h2>$query->num_fields()</h2>
212
 
<p>The number of FIELDS (columns) returned by the query.  Make sure to call the function using your query result object:</p>
213
 
 
214
 
<code>$query = $this->db->query('SELECT * FROM my_table');<br /><br />
215
 
echo $query->num_fields();
216
 
</code>
217
 
 
218
 
 
219
 
 
220
 
<h2>$query->free_result()</h2>
221
 
<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
222
 
execution.  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
223
 
generated in order to cut down on memory consumptions.  Example:
224
 
</p>
225
 
 
226
 
<code>$query = $this->db->query('SELECT title FROM my_table');<br /><br />
227
 
foreach ($query->result() as $row)<br />
228
 
{<br />
229
 
&nbsp;&nbsp;&nbsp;echo $row->title;<br />
230
 
}<br />
231
 
$query->free_result();  // The $query result object will no longer be available<br />
232
 
<br />
233
 
$query2 = $this->db->query('SELECT name FROM some_table');<br /><br />
234
 
$row = $query2->row();<br />
235
 
echo $row->name;<br />
236
 
$query2->free_result();  // The $query2 result object will no longer be available
237
 
</code>
238
 
 
239
 
 
240
 
 
241
 
 
242
 
 
243
 
</div>
244
 
<!-- END CONTENT -->
245
 
 
246
 
 
247
 
<div id="footer">
248
 
<p>
249
 
Previous Topic:&nbsp;&nbsp;<a href="queries.html">Queries</a>
250
 
&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
251
 
<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
252
 
<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
253
 
Next Topic:&nbsp;&nbsp;<a href="helpers.html">Query Helper Functions</a>
254
 
</p>
255
 
<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 - 2012 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
256
 
</div>
257
 
 
258
 
</body>
259
 
</html>