admin | b0dd10f | 2006-08-25 17:25:49 +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 |
|
| 10 | <script type="text/javascript" src="../../scripts/nav.js"></script>
|
| 11 | <script type="text/javascript" src="../../scripts/prototype.lite.js"></script>
|
| 12 | <script type="text/javascript" src="../../scripts/moo.fx.js"></script>
|
| 13 | <script type="text/javascript">
|
| 14 | window.onload = function() {
|
| 15 | myHeight = new fx.Height('nav', {duration: 400});
|
| 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 | 9fc347d | 2006-09-21 00:00:28 +0000 | [diff] [blame] | 36 | <td><h1>Code Igniter User Guide Version 1.4.1</h1></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 37 | <td id="breadcrumb_right"><a href="../../toc.html">Full Table of Contents</a></td>
|
| 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>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>
|
| 87 |
|
| 88 | <p>If you run queries that might <strong>not</strong> produce a result, you are encouraged to test the result first:</p>
|
| 89 |
|
| 90 | <code>
|
| 91 | $query = $this->db->query("YOUR QUERY");<br />
|
| 92 | <br />
|
| 93 | if ($query->num_rows() > 0)<br />
|
| 94 | {<br />
|
| 95 | foreach ($query->result() as $row)<br />
|
| 96 | {<br />
|
| 97 | echo $row->title;<br />
|
| 98 | echo $row->name;<br />
|
| 99 | echo $row->body;<br />
|
| 100 | }<br />
|
| 101 | }
|
| 102 | </code>
|
| 103 |
|
| 104 | <h2>result_array()</h2>
|
| 105 |
|
| 106 | <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>
|
| 107 | <code>
|
| 108 | $query = $this->db->query("YOUR QUERY");<br />
|
| 109 | <br />
|
| 110 | foreach ($query->result_array() as $row)<br />
|
| 111 | {<br />
|
| 112 | echo $row['title'];<br />
|
| 113 | echo $row['name'];<br />
|
| 114 | echo $row['body'];<br />
|
| 115 | }</code>
|
| 116 |
|
| 117 |
|
| 118 | <h2>row()</h2>
|
| 119 |
|
| 120 | <p>This function returns a single result row. If your query has more than one row, it returns only the first row.
|
| 121 | The result is returned as an <strong>object</strong>. Here's a usage example:</p>
|
| 122 | <code>
|
| 123 | $query = $this->db->query("YOUR QUERY");<br />
|
| 124 | <br />
|
| 125 | if ($query->num_rows() > 0)<br />
|
| 126 | {<br />
|
| 127 | $row = $query->row();
|
| 128 | <br /><br />
|
| 129 | echo $row->title;<br />
|
| 130 | echo $row->name;<br />
|
| 131 | echo $row->body;<br />
|
| 132 | }
|
| 133 | </code>
|
| 134 |
|
| 135 | <p>If you want a specific row returned you can submit the row number as a digit in the first parameter:
|
| 136 |
|
| 137 | <code>$row = $query->row(<dfn>5</dfn>);</code>
|
| 138 |
|
| 139 |
|
| 140 | <h2>row_array()</h2>
|
| 141 |
|
| 142 | <p>Identical to the above <var>row()</var> function, except it returns an array. Example:</p>
|
| 143 |
|
| 144 | <code>
|
| 145 | $query = $this->db->query("YOUR QUERY");<br />
|
| 146 | <br />
|
| 147 | if ($query->num_rows() > 0)<br />
|
| 148 | {<br />
|
| 149 | $row = $query->row_array();
|
| 150 | <br /><br />
|
| 151 | echo $row['title'];<br />
|
| 152 | echo $row['name'];<br />
|
| 153 | echo $row['body'];<br />
|
| 154 | }
|
| 155 | </code>
|
| 156 |
|
| 157 |
|
| 158 | <p>If you want a specific row returned you can submit the row number as a digit in the first parameter:
|
| 159 |
|
| 160 | <code>$row = $query->row_array(<dfn>5</dfn>);</code>
|
| 161 |
|
| 162 |
|
| 163 | <p>In addition, you can walk forward/backwards/first/last through your results using these variations:</p>
|
| 164 |
|
| 165 | <p>
|
| 166 | <strong>$row = $query->first_row()</strong><br />
|
| 167 | <strong>$row = $query->last_row()</strong><br />
|
| 168 | <strong>$row = $query->next_row()</strong><br />
|
| 169 | <strong>$row = $query->previous_row()</strong>
|
| 170 | </p>
|
| 171 |
|
| 172 | <p>By default they return an object unless you put the word "array" in the parameter:</p>
|
| 173 |
|
| 174 | <p>
|
| 175 | <strong>$row = $query->first_row('array')</strong><br />
|
| 176 | <strong>$row = $query->last_row('array')</strong><br />
|
| 177 | <strong>$row = $query->next_row('array')</strong><br />
|
| 178 | <strong>$row = $query->previous_row('array')</strong>
|
| 179 | </p>
|
| 180 |
|
| 181 |
|
| 182 | <h1><br />Query Result Helpers</h1>
|
| 183 |
|
| 184 | <p>The following functions provide useful information when dealing with query results.</p>
|
| 185 |
|
| 186 | <h2>$query->num_rows()</h2>
|
| 187 | <p>The number of rows returned by the query. Note: <dfn>$query</dfn> is the variable that the query was assigned to:</p>
|
| 188 |
|
| 189 | <code>$query = $this->db->query('SELECT * FROM my_table');<br /><br />
|
| 190 | echo $query->num_rows();
|
| 191 | </code>
|
| 192 |
|
| 193 | <h2>$query->num_fields()</h2>
|
| 194 | <p>The number of FIELDS returned by the query. Make sure to call the function using your query result object:</p>
|
| 195 |
|
| 196 | <code>$query = $this->db->query('SELECT * FROM my_table');<br /><br />
|
| 197 | echo $query->num_fields();
|
| 198 | </code>
|
| 199 |
|
| 200 |
|
| 201 | <h2>$this->db->insert_id()</h2>
|
| 202 | <p>The insert ID number when performing database inserts.</p>
|
| 203 |
|
| 204 | <h2>$this->db->affected_rows()</h2>
|
| 205 | <p>Displays the number of affected rows, when doing "write" type queries (insert, update, etc.).</p>
|
| 206 | </p>Note: In MySQL "DELETE FROM TABLE" returns 0 affected rows. The database class has a small hack that allows it to return the
|
| 207 | correct number of affected rows. By default this hack is enabled but it can be turned off in the database driver file.</p>
|
| 208 |
|
| 209 |
|
| 210 | <h2>$this->db->version()</h2>
|
| 211 | <p>Outputs the database version you are running:</p>
|
| 212 |
|
| 213 | <code>echo $this->db->version();</code>
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 | </div>
|
| 220 | <!-- END CONTENT -->
|
| 221 |
|
| 222 |
|
| 223 | <div id="footer">
|
| 224 | <p>
|
| 225 | Previous Topic: <a href="queries.html">Queries</a>
|
| 226 | ·
|
| 227 | <a href="#top">Top of Page</a> ·
|
| 228 | <a href="../../index.html">User Guide Home</a> ·
|
| 229 | Next Topic: <a href="active_record.html">Active Record Pattern</a>
|
| 230 | <p>
|
| 231 | <p><a href="http://www.codeigniter.com">Code Igniter</a> · Copyright © 2006 · <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
|
| 232 | </div>
|
| 233 |
|
| 234 | </body>
|
| 235 | </html> |