/lenasys/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/lenasys/trunk

« back to all changes in this revision

Viewing changes to codeigniter/user_guide/database/examples.html

  • Committer: galaxyAbstractor
  • Date: 2013-04-10 15:49:32 UTC
  • mto: (19.1.5 lenasys)
  • mto: This revision was merged to the branch mainline in revision 23.
  • Revision ID: galaxyabstractor@gmail.com-20130410154932-4vizlzk0ar5gykvi
* Added an simple admin panel to the codeviewer-cmssy stuff
* Redesigned a bit like the mockups - still stuff to come
* Implemented the codeviewer + admin panel again using the Framework CodeIgniter instead 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>
 
31
<td><h1>CodeIgniter User Guide Version 2.1.3</h1></td>
 
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> &nbsp;&#8250;&nbsp;
 
45
<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
 
46
<a href="index.html">Database Library</a> &nbsp;&#8250;&nbsp;
 
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&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>
 
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
 
 
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>
 
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 />
 
83
foreach ($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 />
 
90
echo '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 />
 
100
foreach ($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
 
 
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
&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
 
 
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
 
 
140
<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 />
 
148
echo $row['name'];<br />
 
149
</code>
 
150
 
 
151
<p>The above <dfn>row_array()</dfn> function returns an <strong>array</strong>.  Example:  $row['name']</p>
 
152
 
 
153
 
 
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 />
 
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
&nbsp;&nbsp;&nbsp;&nbsp;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
&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>
 
207
Previous 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;
 
211
Next Topic:&nbsp;&nbsp;<a href="configuration.html">Database Configuration</a>
 
212
</p>
 
213
<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 - 2012 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
 
214
</div>
 
215
 
 
216
</body>
 
217
</html>
 
 
b'\\ No newline at end of file'