Derek Allard | 2067d1a | 2008-11-13 22:59:24 +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 xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| 3 | <head> |
| 4 | |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 6 | <title>Database Quick Start : CodeIgniter User Guide</title> |
| 7 | |
| 8 | <style type='text/css' media='all'>@import url('../userguide.css');</style> |
| 9 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> |
| 10 | |
| 11 | <script type="text/javascript" src="../nav/nav.js"></script> |
| 12 | <script type="text/javascript" src="../nav/prototype.lite.js"></script> |
| 13 | <script type="text/javascript" src="../nav/moo.fx.js"></script> |
| 14 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script> |
| 15 | |
| 16 | <meta http-equiv='expires' content='-1' /> |
| 17 | <meta http-equiv= 'pragma' content='no-cache' /> |
| 18 | <meta name='robots' content='all' /> |
| 19 | <meta name='author' content='ExpressionEngine Dev Team' /> |
| 20 | <meta name='description' content='CodeIgniter User Guide' /> |
| 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_darker.jpg" width="154" height="43" 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 Jones | b8c038a | 2011-08-20 08:57:14 -0500 | [diff] [blame^] | 31 | <td><h1>CodeIgniter User Guide Version 2.0.3</h1></td> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 32 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> |
| 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"> |
| 44 | <a href="http://codeigniter.com/">CodeIgniter Home</a> › |
| 45 | <a href="../index.html">User Guide Home</a> › |
| 46 | <a href="index.html">Database Library</a> › |
| 47 | Database Example Code |
| 48 | </td> |
| 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> |
| 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 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 64 | <p>The following page contains example code showing how the database class is used. For complete details please |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 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> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 93 | <p>The above <dfn>result()</dfn> function returns an array of <strong>objects</strong>. Example: $row->title</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 107 | <p>The above <dfn>result_array()</dfn> function returns an array of standard array indexes. Example: $row['title']</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | |
| 109 | |
| 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 | |
| 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 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 140 | <p>The above <dfn>row()</dfn> function returns an <strong>object</strong>. Example: $row->name</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 /> |
| 148 | echo $row['name'];<br /> |
| 149 | </code> |
| 150 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 151 | <p>The above <dfn>row_array()</dfn> function returns an <strong>array</strong>. Example: $row['name']</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | |
| 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> |
| 179 | |
| 180 | <p>The above <dfn>get()</dfn> function retrieves all the results from the supplied table. |
| 181 | The <a href="active_record.html">Active Record</a> class contains a full compliment of functions |
| 182 | for working with data.</p> |
| 183 | |
| 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> |
| 212 | </p> |
Derek Jones | 898949f | 2011-01-28 07:42:16 -0600 | [diff] [blame] | 213 | <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | </div> |
| 215 | |
| 216 | </body> |
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 217 | </html> |