blob: f1df8a77b0a3479214c4f5fd425a4b45c306e86a [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
5<title>Code Igniter User Guide</title>
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
10<script type="text/javascript" src="../scripts/nav.js"></script>
11<script type="text/javascript" src="../scripts/prototype.lite.js"></script>
12<script type="text/javascript" src="../scripts/moo.fx.js"></script>
13<script type="text/javascript">
14window.onload = function() {
15 myHeight = new fx.Height('nav', {duration: 400});
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' />
25<meta name='description' content='Code Igniter User Guide' />
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>
admin41a16852006-09-26 18:17:19 +000036<td><h1>Code Igniter User Guide Version 1.5.0</h1></td>
adminfb28bb82006-09-24 17:59:33 +000037<td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
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">
49<a href="http://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
50<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
115<h2>Standard Query With Single Result</h2>
116
117<code>$query = $this->db->query('SELECT name FROM my_table LIMIT 1');<br />
118<br />
119$row = $query->row();<br />
120echo $row->name;<br />
121</code>
122
123
124<h2>Standard Insert</h2>
125
126<code>
127$sql = "INSERT INTO mytable (title, name) <br />
128&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")";<br />
129<br />
130$this->db->query($sql);<br />
131<br />
132echo $this->db->affected_rows();
133</code>
134
135
136
137
138<h2>Active Record Query</h2>
139
140<p>The <a href="active_record.html">Active Record Pattern</a> gives you a simplified means of retrieving data:</p>
141
142<code>
143$query = $this->db->get('table_name');<br />
144<br />
145foreach ($query->result() as $row)<br />
146{<br />
147&nbsp;&nbsp;&nbsp;&nbsp;echo $row->title;<br />
148}</code>
149
150
151<h2>Active Record Insert</h2>
152
153<code>
154$data = array(<br />
155&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'title' => $title,<br />
156&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'name' => $name,<br />
157&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'date' => $date<br />
158&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
159<br />
160$this->db->insert('mytable', $data);
161<br /><br />
162// Produces: INSERT INTO mytable (title, name, date) VALUES ('{$title}', '{$name}', '{$date}')</code>
163
164
165
166
167</div>
168<!-- END CONTENT -->
169
170
171<div id="footer">
172<p>
173Previous Topic:&nbsp;&nbsp;<a href="index.html">Database Class</a>
174&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
175<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
176<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
177Next Topic:&nbsp;&nbsp;<a href="configuration.html">Database Configuration</a>
178<p>
179<p><a href="http://www.codeigniter.com">Code Igniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 &nbsp;&middot;&nbsp; <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
180</div>
181
182</body>
183</html>