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