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">
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6 <title>Database Quick Start : CodeIgniter User Guide</title>
8 <style type='text/css' media='all'>@import url('../userguide.css');</style>
9 <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
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>
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' />
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>
29 <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
31 <td><h1>CodeIgniter User Guide Version 2.0.2</h1></td>
32 <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
36 <!-- END NAVIGATION -->
40 <!-- START BREADCRUMB -->
41 <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
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> ›
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>
52 <!-- END BREADCRUMB -->
58 <!-- START CONTENT -->
62 <h1>Database Quick Start: Example Code</h1>
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>
68 <h2>Initializing the Database Class</h2>
70 <p>The following code loads and initializes the database class based on your <a href="configuration.html">configuration</a> settings:</p>
72 <code>$this->load->database();</code>
74 <p>Once loaded the class is ready to be used as described below.</p>
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>
79 <h2>Standard Query With Multiple Results (Object Version)</h2>
81 <code>$query = $this->db->query('SELECT name, title, email FROM my_table');<br />
83 foreach ($query->result() as $row)<br />
85 echo $row->title;<br />
86 echo $row->name;<br />
87 echo $row->email;<br />
90 echo 'Total Results: ' . $query->num_rows();
93 <p>The above <dfn>result()</dfn> function returns an array of <strong>objects</strong>. Example: $row->title</p>
96 <h2>Standard Query With Multiple Results (Array Version)</h2>
98 <code>$query = $this->db->query('SELECT name, title, email FROM my_table');<br />
100 foreach ($query->result_array() as $row)<br />
102 echo $row['title'];<br />
103 echo $row['name'];<br />
104 echo $row['email'];<br />
107 <p>The above <dfn>result_array()</dfn> function returns an array of standard array indexes. Example: $row['title']</p>
110 <h2>Testing for Results</h2>
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>
116 $query = $this->db->query("YOUR QUERY");<br />
118 if ($query->num_rows() > 0)<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 />
132 <h2>Standard Query With Single Result</h2>
134 <code>$query = $this->db->query('SELECT name FROM my_table LIMIT 1');<br />
136 $row = $query->row();<br />
137 echo $row->name;<br />
140 <p>The above <dfn>row()</dfn> function returns an <strong>object</strong>. Example: $row->name</p>
143 <h2>Standard Query With Single Result (Array version)</h2>
145 <code>$query = $this->db->query('SELECT name FROM my_table LIMIT 1');<br />
147 $row = $query->row_array();<br />
148 echo $row['name'];<br />
151 <p>The above <dfn>row_array()</dfn> function returns an <strong>array</strong>. Example: $row['name']</p>
154 <h2>Standard Insert</h2>
157 $sql = "INSERT INTO mytable (title, name) <br />
158 VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")";<br />
160 $this->db->query($sql);<br />
162 echo $this->db->affected_rows();
168 <h2>Active Record Query</h2>
170 <p>The <a href="active_record.html">Active Record Pattern</a> gives you a simplified means of retrieving data:</p>
173 $query = $this->db->get('table_name');<br />
175 foreach ($query->result() as $row)<br />
177 echo $row->title;<br />
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>
185 <h2>Active Record Insert</h2>
189 'title' => $title,<br />
190 'name' => $name,<br />
191 'date' => $date<br />
192 );<br />
194 $this->db->insert('mytable', $data);
196 // Produces: INSERT INTO mytable (title, name, date) VALUES ('{$title}', '{$name}', '{$date}')</code>
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>
213 <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p>