blob: 010f9cd1efdf946e7f49076334c60fb4cf6fa168 [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">
2<html>
3<head>
4
Derek Allardd2df9bc2007-04-15 17:41:17 +00005<title>CodeIgniter User Guide</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>
adminfb28bb82006-09-24 17:59:33 +000013<script type="text/javascript">
14window.onload = function() {
admine334c472006-10-21 19:44:22 +000015 myHeight = new fx.Height('nav', {duration: 400});
adminfb28bb82006-09-24 17:59:33 +000016 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 Allardd2df9bc2007-04-15 17:41:17 +000025<meta name='description' content='CodeIgniter User Guide' />
adminfb28bb82006-09-24 17:59:33 +000026
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 Allardd2df9bc2007-04-15 17:41:17 +000036<td><h1>CodeIgniter User Guide Version 1.5.3</h1></td>
adminc0d5d522006-10-30 19:40:35 +000037<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminfb28bb82006-09-24 17:59:33 +000038</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 Allardd2df9bc2007-04-15 17:41:17 +000049<a href="http://www.codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminfb28bb82006-09-24 17:59:33 +000050<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
51<a href="index.html">Database Library</a> &nbsp;&#8250;&nbsp;
52Database Example Code
53</td>
54<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.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>
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
70read 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 />
88foreach ($query->result() as $row)<br />
89{<br />
90&nbsp;&nbsp;&nbsp;&nbsp;echo $row->title;<br />
91&nbsp;&nbsp;&nbsp;&nbsp;echo $row->name;<br />
92&nbsp;&nbsp;&nbsp;&nbsp;echo $row->email;<br />
93}<br />
94<br />
95echo '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 />
105foreach ($query->result_array() as $row)<br />
106{<br />
107&nbsp;&nbsp;&nbsp;&nbsp;echo $row['title'];<br />
108&nbsp;&nbsp;&nbsp;&nbsp;echo $row['name'];<br />
109&nbsp;&nbsp;&nbsp;&nbsp;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
adminb2a9cec2006-10-01 03:38:04 +0000115<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
118using the <dfn>num_rows()</dfn> function:</p>
119
120<code>
121$query = $this->db->query("YOUR QUERY");<br />
122<br />
123if ($query->num_rows() > 0)<br />
124{<br />
125&nbsp;&nbsp;&nbsp;foreach ($query->result() as $row)<br />
126&nbsp;&nbsp;&nbsp;{<br />
127&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $row->title;<br />
128&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $row->name;<br />
129&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $row->body;<br />
130&nbsp;&nbsp;&nbsp;}<br />
131}
132</code>
133
134
135
136
adminfb28bb82006-09-24 17:59:33 +0000137<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 />
142echo $row->name;<br />
143</code>
144
adminb2a9cec2006-10-01 03:38:04 +0000145<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 Allarde1c29d22007-04-03 20:31:07 +0000153echo $row['name'];<br />
adminb2a9cec2006-10-01 03:38:04 +0000154</code>
155
Derek Allarde1c29d22007-04-03 20:31:07 +0000156<p>The above <dfn>row_array()</dfn> function returns an <strong>array</strong>. Example: $row['name']</p>
adminb2a9cec2006-10-01 03:38:04 +0000157
adminfb28bb82006-09-24 17:59:33 +0000158
159<h2>Standard Insert</h2>
160
161<code>
162$sql = "INSERT INTO mytable (title, name) <br />
163&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")";<br />
164<br />
165$this->db->query($sql);<br />
166<br />
167echo $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 />
180foreach ($query->result() as $row)<br />
181{<br />
182&nbsp;&nbsp;&nbsp;&nbsp;echo $row->title;<br />
183}</code>
adminb2a9cec2006-10-01 03:38:04 +0000184
admine334c472006-10-21 19:44:22 +0000185<p>The above <dfn>get()</dfn> function retrieves all the results from the supplied table.
adminb2a9cec2006-10-01 03:38:04 +0000186The <a href="active_record.html">Active Record</a> class contains a full compliment of functions
187for working with data.</p>
188
adminfb28bb82006-09-24 17:59:33 +0000189
190<h2>Active Record Insert</h2>
191
192<code>
193$data = array(<br />
194&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'title' => $title,<br />
195&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'name' => $name,<br />
196&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'date' => $date<br />
197&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<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>
212Previous Topic:&nbsp;&nbsp;<a href="index.html">Database Class</a>
213&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
214<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
215<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
216Next Topic:&nbsp;&nbsp;<a href="configuration.html">Database Configuration</a>
217<p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000218<p><a href="http://www.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 +0000219</div>
220
221</body>
222</html>