blob: 5e06da20a75a0a43e492874c16b998df01c42a62 [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 Allard8039d4c2008-05-31 02:47:56 +00005<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Derek Jonesfd93d222008-05-06 15:18:50 +00006<title>Query Helper Functions : CodeIgniter User Guide</title>
adminfb28bb82006-09-24 17:59:33 +00007
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
admin17a890d2006-09-27 20:42:42 +000011<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000012<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000013<script type="text/javascript" src="../nav/moo.fx.js"></script>
Derek Allardb3412372007-10-25 12:15:16 +000014<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
adminfb28bb82006-09-24 17:59:33 +000015
adminfb28bb82006-09-24 17:59:33 +000016<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 Jones1ca3fc42008-06-27 00:19:33 +000031<td><h1>CodeIgniter User Guide Version 1.6.3</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<!-- START BREADCRUMB -->
40<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
41<tr>
42<td id="breadcrumb">
Derek Jones7a9193a2008-01-21 18:39:20 +000043<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminfb28bb82006-09-24 17:59:33 +000044<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>
Derek Allardbc030912007-06-24 18:25:29 +000048<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 +000049</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>
67
68<h2>$this->db->affected_rows()</h2>
69<p>Displays the number of affected rows, when doing "write" type queries (insert, update, etc.).</p>
Derek Allard0ce27552008-03-18 17:48:06 +000070<p>Note: In MySQL "DELETE FROM TABLE" returns 0 affected rows. The database class has a small hack that allows it to return the
adminfb28bb82006-09-24 17:59:33 +000071correct number of affected rows. By default this hack is enabled but it can be turned off in the database driver file.</p>
72
73
admin910d8622006-09-26 02:09:05 +000074<h2>$this->db->count_all();</h2>
75<p>Permits you to determine the number of rows in a particular table. Submit the table name in the first parameter. Example:</p>
76<code>echo $this->db->count_all('<var>my_table</var>');<br />
77<br />
78// Produces an integer, like 25
79</code>
80
81
admind125c5c2006-09-25 23:26:12 +000082<h2>$this->db->platform()</h2>
83<p>Outputs the database platform you are running (MySQL, MS SQL, Postgre, etc...):</p>
84<code>echo $this->db->platform();</code>
85
86
adminfb28bb82006-09-24 17:59:33 +000087<h2>$this->db->version()</h2>
88<p>Outputs the database version you are running:</p>
adminfb28bb82006-09-24 17:59:33 +000089<code>echo $this->db->version();</code>
90
adminfb28bb82006-09-24 17:59:33 +000091
admind125c5c2006-09-25 23:26:12 +000092<h2>$this->db->last_query();</h2>
adminfb28bb82006-09-24 17:59:33 +000093<p>Returns the last query that was run (the query string, not the result). Example:</p>
94
95<code>$str = $this->db->last_query();<br />
96<br />
97// Produces: SELECT * FROM sometable....
98</code>
99
100
101<p>The following two functions help simplify the process of writing database INSERTs and UPDATEs.</p>
102
103
104<h2>$this->db->insert_string(); </h2>
105<p>This function simplifies the process of writing database inserts. It returns a correctly formatted SQL insert string. Example:</p>
106
107<code>$data = array('name' => $name, 'email' => $email, 'url' => $url);<br />
108<br />
109$str = $this->db->insert_string('table_name', $data);
110</code>
111
112<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 Jonesff845f92008-06-26 17:05:55 +0000113<code>INSERT INTO table_name (name, email, url) VALUES ('Rick', 'rick@example.com', 'example.com')</code>
adminfb28bb82006-09-24 17:59:33 +0000114
Rick Ellis325197e2006-11-20 17:29:05 +0000115<p class="important">Note: Values are automatically escaped, producing safer queries.</p>
116
adminfb28bb82006-09-24 17:59:33 +0000117
118
119<h2>$this->db->update_string(); </h2>
120<p>This function simplifies the process of writing database updates. It returns a correctly formatted SQL update string. Example:</p>
121
122<code>$data = array('name' => $name, 'email' => $email, 'url' => $url);<br />
123<br />
124$where = "author_id = 1 AND status = 'active'";
125<br /><br />
126$str = $this->db->update_string('table_name', $data, $where);
127</code>
128
Derek Allard44bd10f2008-03-25 22:27:37 +0000129<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>
Derek Jonesff845f92008-06-26 17:05:55 +0000130<code> UPDATE table_name SET name = 'Rick', email = 'rick@example.com', url = 'example.com' WHERE author_id = 1 AND status = 'active'</code>
adminfb28bb82006-09-24 17:59:33 +0000131
Rick Ellis325197e2006-11-20 17:29:05 +0000132<p class="important">Note: Values are automatically escaped, producing safer queries.</p>
adminfb28bb82006-09-24 17:59:33 +0000133
134
135</div>
136<!-- END CONTENT -->
137
138
139<div id="footer">
140<p>
141Previous Topic:&nbsp;&nbsp;<a href="results.html">Query Results</a>
142&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
143<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
144<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
145Next Topic:&nbsp;&nbsp;<a href="active_record.html">Active Record Pattern</a>
Derek Allardc6441282007-07-04 23:54:32 +0000146</p>
Derek Jones07870432008-02-13 03:49:26 +0000147<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2008 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
adminfb28bb82006-09-24 17:59:33 +0000148</div>
149
150</body>
151</html>