blob: e7f79ac2a53c472e159b7eb53e871da527b4d255 [file] [log] [blame]
adminfb28bb82006-09-24 17:59:33 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Derek Allardafd99ac2008-01-19 19:59:14 +00002<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
adminfb28bb82006-09-24 17:59:33 +00003<head>
4
Derek Allard404e35d2007-08-07 01:00:45 +00005<title>CodeIgniter User Guide : Database Quick Start</title>
adminfb28bb82006-09-24 17:59:33 +00006
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
admin17a890d2006-09-27 20:42:42 +000010<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000011<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000012<script type="text/javascript" src="../nav/moo.fx.js"></script>
Derek Allardb3412372007-10-25 12:15:16 +000013<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
adminfb28bb82006-09-24 17:59:33 +000014
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 Allard3d879d52008-01-18 19:41:32 +000019<meta name='author' content='ExpressionEngine Dev Team' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000020<meta name='description' content='CodeIgniter User Guide' />
adminfb28bb82006-09-24 17:59:33 +000021
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 Allard39b622d2008-01-16 21:10:09 +000031<td><h1>CodeIgniter User Guide Version 1.6.0</h1></td>
adminc0d5d522006-10-30 19:40:35 +000032<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminfb28bb82006-09-24 17:59:33 +000033</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 Jones7a9193a2008-01-21 18:39:20 +000044<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminfb28bb82006-09-24 17:59:33 +000045<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
46<a href="index.html">Database Library</a> &nbsp;&#8250;&nbsp;
47Database Example Code
48</td>
Derek Allardbc030912007-06-24 18:25:29 +000049<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&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
adminfb28bb82006-09-24 17:59:33 +000050</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
65read 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 />
83foreach ($query->result() as $row)<br />
84{<br />
85&nbsp;&nbsp;&nbsp;&nbsp;echo $row->title;<br />
86&nbsp;&nbsp;&nbsp;&nbsp;echo $row->name;<br />
87&nbsp;&nbsp;&nbsp;&nbsp;echo $row->email;<br />
88}<br />
89<br />
90echo '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 />
100foreach ($query->result_array() as $row)<br />
101{<br />
102&nbsp;&nbsp;&nbsp;&nbsp;echo $row['title'];<br />
103&nbsp;&nbsp;&nbsp;&nbsp;echo $row['name'];<br />
104&nbsp;&nbsp;&nbsp;&nbsp;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
adminb2a9cec2006-10-01 03:38:04 +0000110<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
113using the <dfn>num_rows()</dfn> function:</p>
114
115<code>
116$query = $this->db->query("YOUR QUERY");<br />
117<br />
118if ($query->num_rows() > 0)<br />
119{<br />
120&nbsp;&nbsp;&nbsp;foreach ($query->result() as $row)<br />
121&nbsp;&nbsp;&nbsp;{<br />
122&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $row->title;<br />
123&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $row->name;<br />
124&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $row->body;<br />
125&nbsp;&nbsp;&nbsp;}<br />
126}
127</code>
128
129
130
131
adminfb28bb82006-09-24 17:59:33 +0000132<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 />
137echo $row->name;<br />
138</code>
139
adminb2a9cec2006-10-01 03:38:04 +0000140<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 Allarde1c29d22007-04-03 20:31:07 +0000148echo $row['name'];<br />
adminb2a9cec2006-10-01 03:38:04 +0000149</code>
150
Derek Allarde1c29d22007-04-03 20:31:07 +0000151<p>The above <dfn>row_array()</dfn> function returns an <strong>array</strong>. Example: $row['name']</p>
adminb2a9cec2006-10-01 03:38:04 +0000152
adminfb28bb82006-09-24 17:59:33 +0000153
154<h2>Standard Insert</h2>
155
156<code>
157$sql = "INSERT INTO mytable (title, name) <br />
158&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")";<br />
159<br />
160$this->db->query($sql);<br />
161<br />
162echo $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 />
175foreach ($query->result() as $row)<br />
176{<br />
177&nbsp;&nbsp;&nbsp;&nbsp;echo $row->title;<br />
178}</code>
adminb2a9cec2006-10-01 03:38:04 +0000179
admine334c472006-10-21 19:44:22 +0000180<p>The above <dfn>get()</dfn> function retrieves all the results from the supplied table.
adminb2a9cec2006-10-01 03:38:04 +0000181The <a href="active_record.html">Active Record</a> class contains a full compliment of functions
182for working with data.</p>
183
adminfb28bb82006-09-24 17:59:33 +0000184
185<h2>Active Record Insert</h2>
186
187<code>
188$data = array(<br />
189&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'title' => $title,<br />
190&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'name' => $name,<br />
191&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'date' => $date<br />
192&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<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>
207Previous Topic:&nbsp;&nbsp;<a href="index.html">Database Class</a>
208&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
209<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
210<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
211Next Topic:&nbsp;&nbsp;<a href="configuration.html">Database Configuration</a>
Derek Allardc6441282007-07-04 23:54:32 +0000212</p>
Derek Jones7a9193a2008-01-21 18:39:20 +0000213<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2007 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
adminfb28bb82006-09-24 17:59:33 +0000214</div>
215
216</body>
217</html>