1 <html xmlns=
"http://www.w3.org/1999/xhtml" xml:
lang=
"en">
3 <title>CarbonPHP Documentation
</title>
4 <style type=
"text/css">
11 font-family: "Lucida Sans Unicode", "Lucida Grande";
33 margin: 10px 0 10px 0;
34 list-style-position: inside
;
42 border: 1px dotted
#444;
50 border: 1px solid
#ccc;
61 <h1>Query Results
</h1>
63 <p>There are several ways to generate query results in CarbonPHP.
</p>
69 <p>This method returns the query result as an array of objects, or an empty array on failure. Typically you'll
70 use this in a
<b>foreach ()
</b> look like the following example.
</p>
73 $query = $this-
>db-query('SQL QUERY');
<br />
75 foreach ($query-
>result() as $row)
<br />
77 echo $row-
>title;
<br />
78 echo $row-
>author;
<br />
79 echo $row-
>post;
<br />
83 <p>This method is an alias of the
<b>result_object()
</b> method. If you run queries that may not produce
84 any results you are encouraged to test to see if there are any results.
</p>
87 $query = $this-
>db-
>query('SQL QUERY');
<br />
89 if ($query-
>num_rows()
> 0)
<br />
91 foreach ($query-
>result() as $row)
<br />
92 {<br />
93 echo $row-
>title;
<br />
94 echo $row-
>author;
<br />
95 echo $row-
>post;
<br />
96 }<br />
101 <div class=
"content">
102 <h1>result_array()
</h1>
104 <p>This method returns the query result as a pure array, on an empty array on failure. Typically you will use
105 this in a
<b>foreach ()
</b> loop like the following.
</p>
107 <div class=
"example">
108 $query = $this-
>db-query('SQL QUERY');
<br />
110 foreach ($query-
>result_array() as $row)
<br />
112 echo $row['title'];
<br />
113 echo $row['author'];
<br />
114 echo $row['post'];
<br />
119 <div class=
"content">
122 <p>The method returns a single result row. If you query has more than one result in the row it returns only
123 the first row. The first is returned as an object.
</p>
125 <div class=
"example">
126 $query = $this-
>db-
>query('SQL QUERY');
<br />
128 if ($query-
>num_rows()
> 0)
<br />
130 $row = $query-
>row();
<br />
132 echo $row-
>title;
<br />
133 echo $row-
>author;
<br />
134 echo $row-
>post;
<br />
138 <p>If you wish to return a specific row from the result set you can pass a digit as the parameter.
</p>
140 <div class=
"example">
141 $row = $query-
>row(
<b>4</b>);
145 <div class=
"content">
148 <p>This method is identical to the above
<b>row()
</b> method except that it will return the row as an array.
</p>
150 <div class=
"example">
151 $query = $this-
>db-
>query('SQL QUERY');
<br />
153 if ($query-
>num_rows()
> 0)
<br />
155 $row = $query-
>row_array();
<br />
157 echo $row['title'];
<br />
158 echo $row['author'];
<br />
159 echo $row['post'];
<br />
163 <p>If you wish to return a specific row from the result set you can pass a digit as the parameter.
</p>
165 <div class=
"example">
166 $row = $query-
>row_array(
<b>6</b>);
169 <p>In addition to the above method you can also walk through the results forwards or backwards. You can use
170 the following methods.
</p>
172 <div class=
"example">
173 $row = $query-
>first_row();
<br />
174 $row = $query-
>last_row();
<br />
175 $row = $query-
>next_row();
<br />
176 $row = $query-
>previous_row();
179 <p>By default they return an object unless you pass the parameter '
<b>array
</b>' like the following.
</p>
181 <div class=
"example">
182 $row = $query-
>first_row('array');
<br />
183 $row = $query-
>last_row('array');
<br />
184 $row = $query-
>next_row('array');
<br />
185 $row = $query-
>previous_row('array')
189 <div class=
"content">
192 <p>This method returns the amount of rows that are returned by a query.
</p>
194 <div class=
"example">
195 $query = $this-
>db-
>query('SELECT * FROM `some_table`');
<br />
197 echo $query-
>num_rows();
201 <div class=
"content">
202 <h1>num_fields()
</h1>
204 <p>This method returns the number of fields returned by a query.
</p>
206 <div class=
"example">
207 $query = $this-
>db-
>query('SELECT * FROM `some_table`');
<br />
209 echo $query-
>num_fields();
213 <div class=
"content">
214 <h1>free_result()
</h1>
216 <p>This method will free the memory associated with the result and deletes the results resource ID. Normally
217 PHP frees its memory automatically at the end of the execution of the script. However if you are running many
218 queries in a speicifc script you may wish to free a result after each query to cut down on the memory usage.
</p>
220 <div class=
"example">
221 $query = $this-
>db-
>query('SELECT `title` FROM `some_table`');
<br />
223 foreach ($query-
>result() as $row)
<br />
225 echo $row-
>title;
<br />
228 $query-
>free_result();
230 $query2 = $this-
>db-
>query('SELECT `author` FROM `some_table`');
<br />
232 $row = $query2-
>row();
<br />
233 echo $row-
>author;
<br />
235 $query2-
>free_result();