blob: ed1855ce25f0086541ee4980be01ade2d378b018 [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
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() {
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>
admin10c3f412006-10-08 07:21:12 +000036<td><h1>Code Igniter User Guide Version 1.5.0b1</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<!-- START BREADCRUMB -->
45<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
46<tr>
47<td id="breadcrumb">
48<a href="http://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
49<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
120
121
122<h2>$this->db->update_string(); </h2>
123<p>This function simplifies the process of writing database updates. It returns a correctly formatted SQL update string. Example:</p>
124
125<code>$data = array('name' => $name, 'email' => $email, 'url' => $url);<br />
126<br />
127$where = "author_id = 1 AND status = 'active'";
128<br /><br />
129$str = $this->db->update_string('table_name', $data, $where);
130</code>
131
132<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>
133<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>
134
135
136
137
138</div>
139<!-- END CONTENT -->
140
141
142<div id="footer">
143<p>
144Previous Topic:&nbsp;&nbsp;<a href="results.html">Query Results</a>
145&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
146<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
147<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
148Next Topic:&nbsp;&nbsp;<a href="active_record.html">Active Record Pattern</a>
149<p>
150<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>
151</div>
152
153</body>
154</html>