admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
Derek Allard | afd99ac | 2008-01-19 19:59:14 +0000 | [diff] [blame] | 2 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 3 | <head>
|
| 4 |
|
Derek Allard | 8039d4c | 2008-05-31 02:47:56 +0000 | [diff] [blame] | 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
Derek Jones | fd93d22 | 2008-05-06 15:18:50 +0000 | [diff] [blame] | 6 | <title>Generating Query Results : CodeIgniter User Guide</title>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 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 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 13 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
Derek Allard | b341237 | 2007-10-25 12:15:16 +0000 | [diff] [blame] | 14 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 15 |
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 16 | <meta http-equiv='expires' content='-1' />
|
| 17 | <meta http-equiv= 'pragma' content='no-cache' />
|
| 18 | <meta name='robots' content='all' />
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 19 | <meta name='author' content='ExpressionEngine Dev Team' />
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 20 | <meta name='description' content='CodeIgniter User Guide' />
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 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.jpg" width="153" height="44" 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>
|
Derek Jones | 1ca3fc4 | 2008-06-27 00:19:33 +0000 | [diff] [blame] | 31 | <td><h1>CodeIgniter User Guide Version 1.6.3</h1></td>
|
admin | c0d5d52 | 2006-10-30 19:40:35 +0000 | [diff] [blame] | 32 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 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">
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 43 | <a href="http://codeigniter.com/">CodeIgniter Home</a> ›
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 44 | <a href="../index.html">User Guide Home</a> ›
|
| 45 | <a href="index.html">Database Library</a> ›
|
| 46 | Query Results
|
| 47 | </td>
|
Derek Allard | bc03091 | 2007-06-24 18:25:29 +0000 | [diff] [blame] | 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 <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 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 |
|
Derek Allard | 11b76a3 | 2007-02-02 03:46:31 +0000 | [diff] [blame] | 69 | <p>This function returns the query result as an array of <strong>objects</strong>, or <strong>an empty array</strong> on failure.
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 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 | echo $row->title;<br />
|
| 79 | echo $row->name;<br />
|
| 80 | echo $row->body;<br />
|
| 81 | }</code>
|
admin | b2a9cec | 2006-10-01 03:38:04 +0000 | [diff] [blame] | 82 |
|
| 83 | <p>The above <dfn>function</dfn> is an alias of <dfn>result_object()</dfn>.</p>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 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 | foreach ($query->result() as $row)<br />
|
| 93 | {<br />
|
| 94 | echo $row->title;<br />
|
| 95 | echo $row->name;<br />
|
| 96 | echo $row->body;<br />
|
| 97 | }<br />
|
| 98 | }
|
| 99 | </code>
|
| 100 |
|
| 101 | <h2>result_array()</h2>
|
| 102 |
|
Derek Allard | 67e1053 | 2007-02-24 15:13:17 +0000 | [diff] [blame] | 103 | <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>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 104 | <code>
|
| 105 | $query = $this->db->query("YOUR QUERY");<br />
|
| 106 | <br />
|
| 107 | foreach ($query->result_array() as $row)<br />
|
| 108 | {<br />
|
| 109 | echo $row['title'];<br />
|
| 110 | echo $row['name'];<br />
|
| 111 | echo $row['body'];<br />
|
| 112 | }</code>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 113 |
|
| 114 |
|
| 115 | <h2>row()</h2>
|
| 116 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 117 | <p>This function returns a single result row. If your query has more than one row, it returns only the first row.
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 118 | The result is returned as an <strong>object</strong>. Here's a usage example:</p>
|
| 119 | <code>
|
| 120 | $query = $this->db->query("YOUR QUERY");<br />
|
| 121 | <br />
|
| 122 | if ($query->num_rows() > 0)<br />
|
| 123 | {<br />
|
| 124 | $row = $query->row();
|
| 125 | <br /><br />
|
| 126 | echo $row->title;<br />
|
| 127 | echo $row->name;<br />
|
| 128 | echo $row->body;<br />
|
| 129 | }
|
| 130 | </code>
|
| 131 |
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 132 | <p>If you want a specific row returned you can submit the row number as a digit in the first parameter:</p>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 133 |
|
| 134 | <code>$row = $query->row(<dfn>5</dfn>);</code>
|
| 135 |
|
| 136 |
|
| 137 | <h2>row_array()</h2>
|
| 138 |
|
| 139 | <p>Identical to the above <var>row()</var> function, except it returns an array. Example:</p>
|
| 140 |
|
| 141 | <code>
|
| 142 | $query = $this->db->query("YOUR QUERY");<br />
|
| 143 | <br />
|
| 144 | if ($query->num_rows() > 0)<br />
|
| 145 | {<br />
|
| 146 | $row = $query->row_array();
|
| 147 | <br /><br />
|
| 148 | echo $row['title'];<br />
|
| 149 | echo $row['name'];<br />
|
| 150 | echo $row['body'];<br />
|
| 151 | }
|
| 152 | </code>
|
| 153 |
|
| 154 |
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 155 | <p>If you want a specific row returned you can submit the row number as a digit in the first parameter:</p>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 156 |
|
| 157 | <code>$row = $query->row_array(<dfn>5</dfn>);</code>
|
| 158 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 159 |
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 160 | <p>In addition, you can walk forward/backwards/first/last through your results using these variations:</p>
|
| 161 |
|
| 162 | <p>
|
| 163 | <strong>$row = $query->first_row()</strong><br />
|
| 164 | <strong>$row = $query->last_row()</strong><br />
|
| 165 | <strong>$row = $query->next_row()</strong><br />
|
| 166 | <strong>$row = $query->previous_row()</strong>
|
| 167 | </p>
|
| 168 |
|
| 169 | <p>By default they return an object unless you put the word "array" in the parameter:</p>
|
| 170 |
|
| 171 | <p>
|
| 172 | <strong>$row = $query->first_row('array')</strong><br />
|
| 173 | <strong>$row = $query->last_row('array')</strong><br />
|
| 174 | <strong>$row = $query->next_row('array')</strong><br />
|
| 175 | <strong>$row = $query->previous_row('array')</strong>
|
| 176 | </p>
|
| 177 |
|
| 178 |
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 179 |
|
admin | 78ce3cc | 2006-10-02 02:58:03 +0000 | [diff] [blame] | 180 | <h1>Result Helper Functions</h1>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 181 |
|
| 182 |
|
| 183 | <h2>$query->num_rows()</h2>
|
| 184 | <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>
|
| 185 |
|
| 186 | <code>$query = $this->db->query('SELECT * FROM my_table');<br /><br />
|
| 187 | echo $query->num_rows();
|
| 188 | </code>
|
| 189 |
|
| 190 | <h2>$query->num_fields()</h2>
|
| 191 | <p>The number of FIELDS (columns) returned by the query. Make sure to call the function using your query result object:</p>
|
| 192 |
|
| 193 | <code>$query = $this->db->query('SELECT * FROM my_table');<br /><br />
|
| 194 | echo $query->num_fields();
|
| 195 | </code>
|
| 196 |
|
| 197 |
|
| 198 |
|
admin | ddf83e4 | 2006-09-24 20:25:42 +0000 | [diff] [blame] | 199 | <h2>$query->free_result()</h2>
|
| 200 | <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
|
| 201 | 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
|
| 202 | generated in order to cut down on memory consumptions. Example:
|
| 203 | </p>
|
| 204 |
|
| 205 | <code>$query = $this->db->query('SELECT title FROM my_table');<br /><br />
|
| 206 | foreach ($query->result() as $row)<br />
|
| 207 | {<br />
|
| 208 | echo $row->title;<br />
|
| 209 | }<br />
|
| 210 | $query->free_result(); // The $query result object will no longer be available<br />
|
| 211 | <br />
|
| 212 | $query2 = $this->db->query('SELECT name FROM some_table');<br /><br />
|
| 213 | $row = $query2->row();<br />
|
| 214 | echo $row->name;<br />
|
| 215 | $query2->free_result(); // The $query2 result object will no longer be available
|
| 216 | </code>
|
| 217 |
|
| 218 |
|
| 219 |
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 220 |
|
| 221 |
|
| 222 | </div>
|
| 223 | <!-- END CONTENT -->
|
| 224 |
|
| 225 |
|
| 226 | <div id="footer">
|
| 227 | <p>
|
| 228 | Previous Topic: <a href="queries.html">Queries</a>
|
| 229 | ·
|
| 230 | <a href="#top">Top of Page</a> ·
|
| 231 | <a href="../index.html">User Guide Home</a> ·
|
| 232 | Next Topic: <a href="helpers.html">Query Helper Functions</a>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 233 | </p>
|
Derek Jones | 0787043 | 2008-02-13 03:49:26 +0000 | [diff] [blame] | 234 | <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006-2008 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 235 | </div>
|
| 236 |
|
| 237 | </body>
|
| 238 | </html> |