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 | 404e35d | 2007-08-07 01:00:45 +0000 | [diff] [blame] | 5 | <title>CodeIgniter User Guide : Database Quick Start</title>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 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>
|
Derek Allard | b341237 | 2007-10-25 12:15:16 +0000 | [diff] [blame] | 13 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 14 |
|
| 15 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
| 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 Allard | 197d10b | 2008-02-12 04:20:38 +0000 | [diff] [blame] | 31 | <td><h1>CodeIgniter User Guide Version 1.6.1</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 |
|
| 40 | <!-- START BREADCRUMB -->
|
| 41 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 42 | <tr>
|
| 43 | <td id="breadcrumb">
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 44 | <a href="http://codeigniter.com/">CodeIgniter Home</a> ›
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 45 | <a href="../index.html">User Guide Home</a> ›
|
| 46 | <a href="index.html">Database Library</a> ›
|
| 47 | Database Example Code
|
| 48 | </td>
|
Derek Allard | bc03091 | 2007-06-24 18:25:29 +0000 | [diff] [blame] | 49 | <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] | 50 | </tr>
|
| 51 | </table>
|
| 52 | <!-- END BREADCRUMB -->
|
| 53 |
|
| 54 |
|
| 55 | <br clear="all" />
|
| 56 |
|
| 57 |
|
| 58 | <!-- START CONTENT -->
|
| 59 | <div id="content">
|
| 60 |
|
| 61 |
|
| 62 | <h1>Database Quick Start: Example Code</h1>
|
| 63 |
|
| 64 | <p>The following page contains example code showing how the database class is used. For complete details please
|
| 65 | read the individual pages describing each function.</p>
|
| 66 |
|
| 67 |
|
| 68 | <h2>Initializing the Database Class</h2>
|
| 69 |
|
| 70 | <p>The following code loads and initializes the database class based on your <a href="configuration.html">configuration</a> settings:</p>
|
| 71 |
|
| 72 | <code>$this->load->database();</code>
|
| 73 |
|
| 74 | <p>Once loaded the class is ready to be used as described below.</p>
|
| 75 |
|
| 76 | <p>Note: If all your pages require database access you can connect automatically. See the <a href="connecting.html">connecting</a> page for details.</p>
|
| 77 |
|
| 78 |
|
| 79 | <h2>Standard Query With Multiple Results (Object Version)</h2>
|
| 80 |
|
| 81 | <code>$query = $this->db->query('SELECT name, title, email FROM my_table');<br />
|
| 82 | <br />
|
| 83 | foreach ($query->result() as $row)<br />
|
| 84 | {<br />
|
| 85 | echo $row->title;<br />
|
| 86 | echo $row->name;<br />
|
| 87 | echo $row->email;<br />
|
| 88 | }<br />
|
| 89 | <br />
|
| 90 | echo 'Total Results: ' . $query->num_rows();
|
| 91 | </code>
|
| 92 |
|
| 93 | <p>The above <dfn>result()</dfn> function returns an array of <strong>objects</strong>. Example: $row->title</p>
|
| 94 |
|
| 95 |
|
| 96 | <h2>Standard Query With Multiple Results (Array Version)</h2>
|
| 97 |
|
| 98 | <code>$query = $this->db->query('SELECT name, title, email FROM my_table');<br />
|
| 99 | <br />
|
| 100 | foreach ($query->result_array() as $row)<br />
|
| 101 | {<br />
|
| 102 | echo $row['title'];<br />
|
| 103 | echo $row['name'];<br />
|
| 104 | echo $row['email'];<br />
|
| 105 | }</code>
|
| 106 |
|
| 107 | <p>The above <dfn>result_array()</dfn> function returns an array of standard array indexes. Example: $row['title']</p>
|
| 108 |
|
| 109 |
|
admin | b2a9cec | 2006-10-01 03:38:04 +0000 | [diff] [blame] | 110 | <h2>Testing for Results</h2>
|
| 111 |
|
| 112 | <p>If you run queries that might <strong>not</strong> produce a result, you are encouraged to test for a result first
|
| 113 | using the <dfn>num_rows()</dfn> function:</p>
|
| 114 |
|
| 115 | <code>
|
| 116 | $query = $this->db->query("YOUR QUERY");<br />
|
| 117 | <br />
|
| 118 | if ($query->num_rows() > 0)<br />
|
| 119 | {<br />
|
| 120 | foreach ($query->result() as $row)<br />
|
| 121 | {<br />
|
| 122 | echo $row->title;<br />
|
| 123 | echo $row->name;<br />
|
| 124 | echo $row->body;<br />
|
| 125 | }<br />
|
| 126 | }
|
| 127 | </code>
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 132 | <h2>Standard Query With Single Result</h2>
|
| 133 |
|
| 134 | <code>$query = $this->db->query('SELECT name FROM my_table LIMIT 1');<br />
|
| 135 | <br />
|
| 136 | $row = $query->row();<br />
|
| 137 | echo $row->name;<br />
|
| 138 | </code>
|
| 139 |
|
admin | b2a9cec | 2006-10-01 03:38:04 +0000 | [diff] [blame] | 140 | <p>The above <dfn>row()</dfn> function returns an <strong>object</strong>. Example: $row->name</p>
|
| 141 |
|
| 142 |
|
| 143 | <h2>Standard Query With Single Result (Array version)</h2>
|
| 144 |
|
| 145 | <code>$query = $this->db->query('SELECT name FROM my_table LIMIT 1');<br />
|
| 146 | <br />
|
| 147 | $row = $query->row_array();<br />
|
Derek Allard | e1c29d2 | 2007-04-03 20:31:07 +0000 | [diff] [blame] | 148 | echo $row['name'];<br />
|
admin | b2a9cec | 2006-10-01 03:38:04 +0000 | [diff] [blame] | 149 | </code>
|
| 150 |
|
Derek Allard | e1c29d2 | 2007-04-03 20:31:07 +0000 | [diff] [blame] | 151 | <p>The above <dfn>row_array()</dfn> function returns an <strong>array</strong>. Example: $row['name']</p>
|
admin | b2a9cec | 2006-10-01 03:38:04 +0000 | [diff] [blame] | 152 |
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 153 |
|
| 154 | <h2>Standard Insert</h2>
|
| 155 |
|
| 156 | <code>
|
| 157 | $sql = "INSERT INTO mytable (title, name) <br />
|
| 158 | VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")";<br />
|
| 159 | <br />
|
| 160 | $this->db->query($sql);<br />
|
| 161 | <br />
|
| 162 | echo $this->db->affected_rows();
|
| 163 | </code>
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 | <h2>Active Record Query</h2>
|
| 169 |
|
| 170 | <p>The <a href="active_record.html">Active Record Pattern</a> gives you a simplified means of retrieving data:</p>
|
| 171 |
|
| 172 | <code>
|
| 173 | $query = $this->db->get('table_name');<br />
|
| 174 | <br />
|
| 175 | foreach ($query->result() as $row)<br />
|
| 176 | {<br />
|
| 177 | echo $row->title;<br />
|
| 178 | }</code>
|
admin | b2a9cec | 2006-10-01 03:38:04 +0000 | [diff] [blame] | 179 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 180 | <p>The above <dfn>get()</dfn> function retrieves all the results from the supplied table.
|
admin | b2a9cec | 2006-10-01 03:38:04 +0000 | [diff] [blame] | 181 | The <a href="active_record.html">Active Record</a> class contains a full compliment of functions
|
| 182 | for working with data.</p>
|
| 183 |
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 184 |
|
| 185 | <h2>Active Record Insert</h2>
|
| 186 |
|
| 187 | <code>
|
| 188 | $data = array(<br />
|
| 189 | 'title' => $title,<br />
|
| 190 | 'name' => $name,<br />
|
| 191 | 'date' => $date<br />
|
| 192 | );<br />
|
| 193 | <br />
|
| 194 | $this->db->insert('mytable', $data);
|
| 195 | <br /><br />
|
| 196 | // Produces: INSERT INTO mytable (title, name, date) VALUES ('{$title}', '{$name}', '{$date}')</code>
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 | </div>
|
| 202 | <!-- END CONTENT -->
|
| 203 |
|
| 204 |
|
| 205 | <div id="footer">
|
| 206 | <p>
|
| 207 | Previous Topic: <a href="index.html">Database Class</a>
|
| 208 | ·
|
| 209 | <a href="#top">Top of Page</a> ·
|
| 210 | <a href="../index.html">User Guide Home</a> ·
|
| 211 | Next Topic: <a href="configuration.html">Database Configuration</a>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 212 | </p>
|
Derek Jones | 0787043 | 2008-02-13 03:49:26 +0000 | [diff] [blame] | 213 | <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] | 214 | </div>
|
| 215 |
|
| 216 | </body>
|
| 217 | </html> |