blob: 685dd8d44b26c4b2e9b14d125141867932a0ba97 [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<!-- START BREADCRUMB -->
45<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
46<tr>
47<td id="breadcrumb">
Derek Allardd2df9bc2007-04-15 17:41:17 +000048<a href="http://www.codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminfb28bb82006-09-24 17:59:33 +000049<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50<a href="index.html">Database Library</a> &nbsp;&#8250;&nbsp;
51Query Helpers
52</td>
53<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>
54</tr>
55</table>
56<!-- END BREADCRUMB -->
57
58
59
60<br clear="all" />
61
62
63<!-- START CONTENT -->
64<div id="content">
65
66
67<h1>Query Helper Functions</h1>
68
69
70<h2>$this->db->insert_id()</h2>
71<p>The insert ID number when performing database inserts.</p>
72
73<h2>$this->db->affected_rows()</h2>
74<p>Displays the number of affected rows, when doing "write" type queries (insert, update, etc.).</p>
75</p>Note: In MySQL "DELETE FROM TABLE" returns 0 affected rows. The database class has a small hack that allows it to return the
76correct number of affected rows. By default this hack is enabled but it can be turned off in the database driver file.</p>
77
78
admin910d8622006-09-26 02:09:05 +000079<h2>$this->db->count_all();</h2>
80<p>Permits you to determine the number of rows in a particular table. Submit the table name in the first parameter. Example:</p>
81<code>echo $this->db->count_all('<var>my_table</var>');<br />
82<br />
83// Produces an integer, like 25
84</code>
85
86
admind125c5c2006-09-25 23:26:12 +000087<h2>$this->db->platform()</h2>
88<p>Outputs the database platform you are running (MySQL, MS SQL, Postgre, etc...):</p>
89<code>echo $this->db->platform();</code>
90
91
adminfb28bb82006-09-24 17:59:33 +000092<h2>$this->db->version()</h2>
93<p>Outputs the database version you are running:</p>
adminfb28bb82006-09-24 17:59:33 +000094<code>echo $this->db->version();</code>
95
adminfb28bb82006-09-24 17:59:33 +000096
admind125c5c2006-09-25 23:26:12 +000097<h2>$this->db->last_query();</h2>
adminfb28bb82006-09-24 17:59:33 +000098<p>Returns the last query that was run (the query string, not the result). Example:</p>
99
100<code>$str = $this->db->last_query();<br />
101<br />
102// Produces: SELECT * FROM sometable....
103</code>
104
105
106<p>The following two functions help simplify the process of writing database INSERTs and UPDATEs.</p>
107
108
109<h2>$this->db->insert_string(); </h2>
110<p>This function simplifies the process of writing database inserts. It returns a correctly formatted SQL insert string. Example:</p>
111
112<code>$data = array('name' => $name, 'email' => $email, 'url' => $url);<br />
113<br />
114$str = $this->db->insert_string('table_name', $data);
115</code>
116
117<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>
118<code>INSERT INTO table_name (name, email, url) VALUES ('Rick', 'rick@your-site.com', 'www.your-site.com')</code>
119
Rick Ellis325197e2006-11-20 17:29:05 +0000120<p class="important">Note: Values are automatically escaped, producing safer queries.</p>
121
adminfb28bb82006-09-24 17:59:33 +0000122
123
124<h2>$this->db->update_string(); </h2>
125<p>This function simplifies the process of writing database updates. It returns a correctly formatted SQL update string. Example:</p>
126
127<code>$data = array('name' => $name, 'email' => $email, 'url' => $url);<br />
128<br />
129$where = "author_id = 1 AND status = 'active'";
130<br /><br />
131$str = $this->db->update_string('table_name', $data, $where);
132</code>
133
134<p>The first parameter is the table name, the second is an associative array with the data to be inserted, and the third parameter is the "where" clause. The above example produces:</p>
135<code> UPDATE exp_weblog SET name = 'Rick', email = 'rick@your-site.com', url = 'www.your-site.com' WHERE author_id = 1 AND status = 'active'</code>
136
Rick Ellis325197e2006-11-20 17:29:05 +0000137<p class="important">Note: Values are automatically escaped, producing safer queries.</p>
adminfb28bb82006-09-24 17:59:33 +0000138
139
140</div>
141<!-- END CONTENT -->
142
143
144<div id="footer">
145<p>
146Previous Topic:&nbsp;&nbsp;<a href="results.html">Query Results</a>
147&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
148<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
149<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
150Next Topic:&nbsp;&nbsp;<a href="active_record.html">Active Record Pattern</a>
151<p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000152<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 +0000153</div>
154
155</body>
156</html>