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 |
|
| 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>
|
| 36 | <td><h1>Code Igniter User Guide Version 1.4.1</h1></td>
|
| 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>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>
|
| 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 | <h2>result('array')</h2>
|
| 118 |
|
| 119 | <p>Identical to <dfn>$this->db->result_array()</dfn>.</p>
|
| 120 |
|
| 121 |
|
| 122 | <h2>row()</h2>
|
| 123 |
|
| 124 | <p>This function returns a single result row. If your query has more than one row, it returns only the first row.
|
| 125 | The result is returned as an <strong>object</strong>. Here's a usage example:</p>
|
| 126 | <code>
|
| 127 | $query = $this->db->query("YOUR QUERY");<br />
|
| 128 | <br />
|
| 129 | if ($query->num_rows() > 0)<br />
|
| 130 | {<br />
|
| 131 | $row = $query->row();
|
| 132 | <br /><br />
|
| 133 | echo $row->title;<br />
|
| 134 | echo $row->name;<br />
|
| 135 | echo $row->body;<br />
|
| 136 | }
|
| 137 | </code>
|
| 138 |
|
| 139 | <p>If you want a specific row returned you can submit the row number as a digit in the first parameter:
|
| 140 |
|
| 141 | <code>$row = $query->row(<dfn>5</dfn>);</code>
|
| 142 |
|
| 143 |
|
| 144 | <h2>row_array()</h2>
|
| 145 |
|
| 146 | <p>Identical to the above <var>row()</var> function, except it returns an array. Example:</p>
|
| 147 |
|
| 148 | <code>
|
| 149 | $query = $this->db->query("YOUR QUERY");<br />
|
| 150 | <br />
|
| 151 | if ($query->num_rows() > 0)<br />
|
| 152 | {<br />
|
| 153 | $row = $query->row_array();
|
| 154 | <br /><br />
|
| 155 | echo $row['title'];<br />
|
| 156 | echo $row['name'];<br />
|
| 157 | echo $row['body'];<br />
|
| 158 | }
|
| 159 | </code>
|
| 160 |
|
| 161 |
|
| 162 | <p>If you want a specific row returned you can submit the row number as a digit in the first parameter:
|
| 163 |
|
| 164 | <code>$row = $query->row_array(<dfn>5</dfn>);</code>
|
| 165 |
|
| 166 |
|
| 167 | <p>In addition, you can walk forward/backwards/first/last through your results using these variations:</p>
|
| 168 |
|
| 169 | <p>
|
| 170 | <strong>$row = $query->first_row()</strong><br />
|
| 171 | <strong>$row = $query->last_row()</strong><br />
|
| 172 | <strong>$row = $query->next_row()</strong><br />
|
| 173 | <strong>$row = $query->previous_row()</strong>
|
| 174 | </p>
|
| 175 |
|
| 176 | <p>By default they return an object unless you put the word "array" in the parameter:</p>
|
| 177 |
|
| 178 | <p>
|
| 179 | <strong>$row = $query->first_row('array')</strong><br />
|
| 180 | <strong>$row = $query->last_row('array')</strong><br />
|
| 181 | <strong>$row = $query->next_row('array')</strong><br />
|
| 182 | <strong>$row = $query->previous_row('array')</strong>
|
| 183 | </p>
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 | <h1><br />Result Helper Functions</h1>
|
| 188 |
|
| 189 |
|
| 190 | <h2>$query->num_rows()</h2>
|
| 191 | <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>
|
| 192 |
|
| 193 | <code>$query = $this->db->query('SELECT * FROM my_table');<br /><br />
|
| 194 | echo $query->num_rows();
|
| 195 | </code>
|
| 196 |
|
| 197 | <h2>$query->num_fields()</h2>
|
| 198 | <p>The number of FIELDS (columns) returned by the query. Make sure to call the function using your query result object:</p>
|
| 199 |
|
| 200 | <code>$query = $this->db->query('SELECT * FROM my_table');<br /><br />
|
| 201 | echo $query->num_fields();
|
| 202 | </code>
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 | </div>
|
| 209 | <!-- END CONTENT -->
|
| 210 |
|
| 211 |
|
| 212 | <div id="footer">
|
| 213 | <p>
|
| 214 | Previous Topic: <a href="queries.html">Queries</a>
|
| 215 | ·
|
| 216 | <a href="#top">Top of Page</a> ·
|
| 217 | <a href="../index.html">User Guide Home</a> ·
|
| 218 | Next Topic: <a href="helpers.html">Query Helper Functions</a>
|
| 219 | <p>
|
| 220 | <p><a href="http://www.codeigniter.com">Code Igniter</a> · Copyright © 2006 · <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
|
| 221 | </div>
|
| 222 |
|
| 223 | </body>
|
| 224 | </html> |