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">
|
| 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 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 10 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 13 | <script type="text/javascript">
|
| 14 | window.onload = function() {
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | myHeight = new fx.Height('nav', {duration: 400});
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 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>
|
admin | 0518661 | 2006-10-28 03:28:01 +0000 | [diff] [blame] | 36 | <td><h1>Code Igniter User Guide Version 1.5.0</h1></td>
|
admin | c0d5d52 | 2006-10-30 19:40:35 +0000 | [diff] [blame] | 37 | <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] | 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> ›
|
| 49 | <a href="../index.html">User Guide Home</a> ›
|
| 50 | <a href="index.html">Database Library</a> ›
|
| 51 | Query 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 <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>
|
| 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 | echo $row->title;<br />
|
| 84 | echo $row->name;<br />
|
| 85 | echo $row->body;<br />
|
| 86 | }</code>
|
admin | b2a9cec | 2006-10-01 03:38:04 +0000 | [diff] [blame] | 87 |
|
| 88 | <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] | 89 |
|
| 90 | <p>If you run queries that might <strong>not</strong> produce a result, you are encouraged to test the result first:</p>
|
| 91 |
|
| 92 | <code>
|
| 93 | $query = $this->db->query("YOUR QUERY");<br />
|
| 94 | <br />
|
| 95 | if ($query->num_rows() > 0)<br />
|
| 96 | {<br />
|
| 97 | foreach ($query->result() as $row)<br />
|
| 98 | {<br />
|
| 99 | echo $row->title;<br />
|
| 100 | echo $row->name;<br />
|
| 101 | echo $row->body;<br />
|
| 102 | }<br />
|
| 103 | }
|
| 104 | </code>
|
| 105 |
|
| 106 | <h2>result_array()</h2>
|
| 107 |
|
| 108 | <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>
|
| 109 | <code>
|
| 110 | $query = $this->db->query("YOUR QUERY");<br />
|
| 111 | <br />
|
| 112 | foreach ($query->result_array() as $row)<br />
|
| 113 | {<br />
|
| 114 | echo $row['title'];<br />
|
| 115 | echo $row['name'];<br />
|
| 116 | echo $row['body'];<br />
|
| 117 | }</code>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 118 |
|
| 119 |
|
| 120 | <h2>row()</h2>
|
| 121 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 122 | <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] | 123 | The result is returned as an <strong>object</strong>. Here's a usage example:</p>
|
| 124 | <code>
|
| 125 | $query = $this->db->query("YOUR QUERY");<br />
|
| 126 | <br />
|
| 127 | if ($query->num_rows() > 0)<br />
|
| 128 | {<br />
|
| 129 | $row = $query->row();
|
| 130 | <br /><br />
|
| 131 | echo $row->title;<br />
|
| 132 | echo $row->name;<br />
|
| 133 | echo $row->body;<br />
|
| 134 | }
|
| 135 | </code>
|
| 136 |
|
| 137 | <p>If you want a specific row returned you can submit the row number as a digit in the first parameter:
|
| 138 |
|
| 139 | <code>$row = $query->row(<dfn>5</dfn>);</code>
|
| 140 |
|
| 141 |
|
| 142 | <h2>row_array()</h2>
|
| 143 |
|
| 144 | <p>Identical to the above <var>row()</var> function, except it returns an array. Example:</p>
|
| 145 |
|
| 146 | <code>
|
| 147 | $query = $this->db->query("YOUR QUERY");<br />
|
| 148 | <br />
|
| 149 | if ($query->num_rows() > 0)<br />
|
| 150 | {<br />
|
| 151 | $row = $query->row_array();
|
| 152 | <br /><br />
|
| 153 | echo $row['title'];<br />
|
| 154 | echo $row['name'];<br />
|
| 155 | echo $row['body'];<br />
|
| 156 | }
|
| 157 | </code>
|
| 158 |
|
| 159 |
|
| 160 | <p>If you want a specific row returned you can submit the row number as a digit in the first parameter:
|
| 161 |
|
| 162 | <code>$row = $query->row_array(<dfn>5</dfn>);</code>
|
| 163 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 164 |
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 165 | <p>In addition, you can walk forward/backwards/first/last through your results using these variations:</p>
|
| 166 |
|
| 167 | <p>
|
| 168 | <strong>$row = $query->first_row()</strong><br />
|
| 169 | <strong>$row = $query->last_row()</strong><br />
|
| 170 | <strong>$row = $query->next_row()</strong><br />
|
| 171 | <strong>$row = $query->previous_row()</strong>
|
| 172 | </p>
|
| 173 |
|
| 174 | <p>By default they return an object unless you put the word "array" in the parameter:</p>
|
| 175 |
|
| 176 | <p>
|
| 177 | <strong>$row = $query->first_row('array')</strong><br />
|
| 178 | <strong>$row = $query->last_row('array')</strong><br />
|
| 179 | <strong>$row = $query->next_row('array')</strong><br />
|
| 180 | <strong>$row = $query->previous_row('array')</strong>
|
| 181 | </p>
|
| 182 |
|
| 183 |
|
admin | 78ce3cc | 2006-10-02 02:58:03 +0000 | [diff] [blame] | 184 | <br />
|
| 185 | <h1>Result Helper Functions</h1>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 186 |
|
| 187 |
|
| 188 | <h2>$query->num_rows()</h2>
|
| 189 | <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>
|
| 190 |
|
| 191 | <code>$query = $this->db->query('SELECT * FROM my_table');<br /><br />
|
| 192 | echo $query->num_rows();
|
| 193 | </code>
|
| 194 |
|
| 195 | <h2>$query->num_fields()</h2>
|
| 196 | <p>The number of FIELDS (columns) returned by the query. Make sure to call the function using your query result object:</p>
|
| 197 |
|
| 198 | <code>$query = $this->db->query('SELECT * FROM my_table');<br /><br />
|
| 199 | echo $query->num_fields();
|
| 200 | </code>
|
| 201 |
|
| 202 |
|
| 203 |
|
admin | ddf83e4 | 2006-09-24 20:25:42 +0000 | [diff] [blame] | 204 | <h2>$query->free_result()</h2>
|
| 205 | <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
|
| 206 | 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
|
| 207 | generated in order to cut down on memory consumptions. Example:
|
| 208 | </p>
|
| 209 |
|
| 210 | <code>$query = $this->db->query('SELECT title FROM my_table');<br /><br />
|
| 211 | foreach ($query->result() as $row)<br />
|
| 212 | {<br />
|
| 213 | echo $row->title;<br />
|
| 214 | }<br />
|
| 215 | $query->free_result(); // The $query result object will no longer be available<br />
|
| 216 | <br />
|
| 217 | $query2 = $this->db->query('SELECT name FROM some_table');<br /><br />
|
| 218 | $row = $query2->row();<br />
|
| 219 | echo $row->name;<br />
|
| 220 | $query2->free_result(); // The $query2 result object will no longer be available
|
| 221 | </code>
|
| 222 |
|
| 223 |
|
| 224 |
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 225 |
|
| 226 |
|
| 227 | </div>
|
| 228 | <!-- END CONTENT -->
|
| 229 |
|
| 230 |
|
| 231 | <div id="footer">
|
| 232 | <p>
|
| 233 | Previous Topic: <a href="queries.html">Queries</a>
|
| 234 | ·
|
| 235 | <a href="#top">Top of Page</a> ·
|
| 236 | <a href="../index.html">User Guide Home</a> ·
|
| 237 | Next Topic: <a href="helpers.html">Query Helper Functions</a>
|
| 238 | <p>
|
| 239 | <p><a href="http://www.codeigniter.com">Code Igniter</a> · Copyright © 2006 · <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
|
| 240 | </div>
|
| 241 |
|
| 242 | </body>
|
| 243 | </html> |