blob: ecd0d84b6246f987be8c1ec006d681dd2744600f [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<!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>Query Helper Functions : 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>
Derek Jonesb8c038a2011-08-20 08:57:14 -050031<td><h1>CodeIgniter User Guide Version 2.0.3</h1></td>
Derek Allard2067d1a2008-11-13 22:59:24 +000032<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<!-- START BREADCRUMB -->
40<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
41<tr>
42<td id="breadcrumb">
43<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
44<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45<a href="index.html">Database Library</a> &nbsp;&#8250;&nbsp;
46Query Helpers
47</td>
48<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>
49</tr>
50</table>
51<!-- END BREADCRUMB -->
52
53
54
55<br clear="all" />
56
57
58<!-- START CONTENT -->
59<div id="content">
60
61
62<h1>Query Helper Functions</h1>
63
64
65<h2>$this->db->insert_id()</h2>
66<p>The insert ID number when performing database inserts.</p>
Timothy Warren51a48882011-09-14 13:47:06 -040067<p class="important"><strong>Note:</strong> If using the PDO driver with PostgreSQL, this function requires a $name parameter, which specifies the appropriate sequence to check for the insert id.</p>
Derek Allard2067d1a2008-11-13 22:59:24 +000068
69<h2>$this->db->affected_rows()</h2>
70<p>Displays the number of affected rows, when doing "write" type queries (insert, update, etc.).</p>
Derek Jones4b9c6292011-07-01 17:40:48 -050071<p>Note: In MySQL "DELETE FROM TABLE" returns 0 affected rows. The database class has a small hack that allows it to return the
72correct number of affected rows. By default this hack is enabled but it can be turned off in the database driver file.</p>
Derek Allard2067d1a2008-11-13 22:59:24 +000073
74
75<h2>$this->db->count_all();</h2>
Derek Jones4b9c6292011-07-01 17:40:48 -050076<p>Permits you to determine the number of rows in a particular table. Submit the table name in the first parameter. Example:</p>
Derek Allard2067d1a2008-11-13 22:59:24 +000077<code>echo $this->db->count_all('<var>my_table</var>');<br />
78<br />
79// Produces an integer, like 25
80</code>
81
82
83<h2>$this->db->platform()</h2>
84<p>Outputs the database platform you are running (MySQL, MS SQL, Postgres, etc...):</p>
85<code>echo $this->db->platform();</code>
86
87
88<h2>$this->db->version()</h2>
89<p>Outputs the database version you are running:</p>
90<code>echo $this->db->version();</code>
91
92
93<h2>$this->db->last_query();</h2>
Derek Jones4b9c6292011-07-01 17:40:48 -050094<p>Returns the last query that was run (the query string, not the result). Example:</p>
Derek Allard2067d1a2008-11-13 22:59:24 +000095
96<code>$str = $this->db->last_query();<br />
97<br />
Derek Jones4b9c6292011-07-01 17:40:48 -050098// Produces: SELECT * FROM sometable....
Derek Allard2067d1a2008-11-13 22:59:24 +000099</code>
100
101
102<p>The following two functions help simplify the process of writing database INSERTs and UPDATEs.</p>
103
104
105<h2>$this->db->insert_string(); </h2>
106<p>This function simplifies the process of writing database inserts. It returns a correctly formatted SQL insert string. Example:</p>
107
108<code>$data = array('name' => $name, 'email' => $email, 'url' => $url);<br />
109<br />
110$str = $this->db->insert_string('table_name', $data);
111</code>
112
Derek Jones4b9c6292011-07-01 17:40:48 -0500113<p>The first parameter is the table name, the second is an associative array with the data to be inserted. The above example produces:</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000114<code>INSERT INTO table_name (name, email, url) VALUES ('Rick', 'rick@example.com', 'example.com')</code>
115
116<p class="important">Note: Values are automatically escaped, producing safer queries.</p>
117
118
119
120<h2>$this->db->update_string(); </h2>
121<p>This function simplifies the process of writing database updates. It returns a correctly formatted SQL update string. Example:</p>
122
123<code>$data = array('name' => $name, 'email' => $email, 'url' => $url);<br />
124<br />
125$where = "author_id = 1 AND status = 'active'";
126<br /><br />
127$str = $this->db->update_string('table_name', $data, $where);
128</code>
129
130<p>The first parameter is the table name, the second is an associative array with the data to be updated, and the third parameter is the "where" clause. The above example produces:</p>
131<code> UPDATE table_name SET name = 'Rick', email = 'rick@example.com', url = 'example.com' WHERE author_id = 1 AND status = 'active'</code>
132
133<p class="important">Note: Values are automatically escaped, producing safer queries.</p>
134
135
136</div>
137<!-- END CONTENT -->
138
139
140<div id="footer">
141<p>
142Previous Topic:&nbsp;&nbsp;<a href="results.html">Query Results</a>
143&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
144<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
145<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
146Next Topic:&nbsp;&nbsp;<a href="active_record.html">Active Record Pattern</a>
147</p>
Derek Jones898949f2011-01-28 07:42:16 -0600148<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 - 2011 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000149</div>
150
151</body>
adminfb28bb82006-09-24 17:59:33 +0000152</html>