diff options
Diffstat (limited to 'user_guide/database/results.html')
-rw-r--r-- | user_guide/database/results.html | 45 |
1 files changed, 33 insertions, 12 deletions
diff --git a/user_guide/database/results.html b/user_guide/database/results.html index 75cb190f9..0b82752a7 100644 --- a/user_guide/database/results.html +++ b/user_guide/database/results.html @@ -28,7 +28,7 @@ <div id="masthead"> <table cellpadding="0" cellspacing="0" border="0" style="width:100%"> <tr> -<td><h1>CodeIgniter User Guide Version 2.0.0</h1></td> +<td><h1>CodeIgniter User Guide Version 2.0.2</h1></td> <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> </tr> </table> @@ -98,9 +98,21 @@ Query Results } </code> + <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> + + <code> + $query = $this->db->query("SELECT * FROM users;");<br /> + <br /> + foreach ($query->result('User') as $user)<br /> + {<br /> + echo $row->name; // call attributes<br /> + echo $row->reverse_name(); // or methods defined on the 'User' class<br /> + } + </code> + <h2>result_array()</h2> - <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> + <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> <code> $query = $this->db->query("YOUR QUERY");<br /> <br /> @@ -114,8 +126,8 @@ Query Results <h2>row()</h2> - <p>This function returns a single result row. If your query has more than one row, it returns only the first row. - The result is returned as an <strong>object</strong>. Here's a usage example:</p> + <p>This function returns a single result row. If your query has more than one row, it returns only the first row. + The result is returned as an <strong>object</strong>. Here's a usage example:</p> <code> $query = $this->db->query("YOUR QUERY");<br /> <br /> @@ -133,10 +145,19 @@ Query Results <code>$row = $query->row(<dfn>5</dfn>);</code> + <p>You can also add a second String parameter, which is the name of a class to instantiate the row with:</p> + + <code> + $query = $this->db->query("SELECT * FROM users LIMIT 1;");<br /> + <br /> + $query->row(0, 'User')<br /> + echo $row->name; // call attributes<br /> + echo $row->reverse_name(); // or methods defined on the 'User' class<br /> + </code> <h2>row_array()</h2> - <p>Identical to the above <var>row()</var> function, except it returns an array. Example:</p> + <p>Identical to the above <var>row()</var> function, except it returns an array. Example:</p> <code> $query = $this->db->query("YOUR QUERY");<br /> @@ -188,7 +209,7 @@ echo $query->num_rows(); </code> <h2>$query->num_fields()</h2> -<p>The number of FIELDS (columns) returned by the query. Make sure to call the function using your query result object:</p> +<p>The number of FIELDS (columns) returned by the query. Make sure to call the function using your query result object:</p> <code>$query = $this->db->query('SELECT * FROM my_table');<br /><br /> echo $query->num_fields(); @@ -197,9 +218,9 @@ echo $query->num_fields(); <h2>$query->free_result()</h2> -<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 -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 -generated in order to cut down on memory consumptions. Example: +<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 +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 +generated in order to cut down on memory consumptions. Example: </p> <code>$query = $this->db->query('SELECT title FROM my_table');<br /><br /> @@ -207,12 +228,12 @@ foreach ($query->result() as $row)<br /> {<br /> echo $row->title;<br /> }<br /> -$query->free_result(); // The $query result object will no longer be available<br /> +$query->free_result(); // The $query result object will no longer be available<br /> <br /> $query2 = $this->db->query('SELECT name FROM some_table');<br /><br /> $row = $query2->row();<br /> echo $row->name;<br /> -$query2->free_result(); // The $query2 result object will no longer be available +$query2->free_result(); // The $query2 result object will no longer be available </code> @@ -235,4 +256,4 @@ Next Topic: <a href="helpers.html">Query Helper Functions</a> </div> </body> -</html>
\ No newline at end of file +</html> |