blob: 1d1fb37ad4d19fad1e1695adbb869bcee5103293 [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
10<script type="text/javascript" src="../scripts/nav.js"></script>
11<script type="text/javascript" src="../scripts/prototype.lite.js"></script>
12<script type="text/javascript" src="../scripts/moo.fx.js"></script>
13<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>
36<td><h1>Code Igniter User Guide Version 1.4.1</h1></td>
37<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
admind125c5c2006-09-25 23:26:12 +000079<h2>$this->db->platform()</h2>
80<p>Outputs the database platform you are running (MySQL, MS SQL, Postgre, etc...):</p>
81<code>echo $this->db->platform();</code>
82
83
adminfb28bb82006-09-24 17:59:33 +000084<h2>$this->db->version()</h2>
85<p>Outputs the database version you are running:</p>
adminfb28bb82006-09-24 17:59:33 +000086<code>echo $this->db->version();</code>
87
adminfb28bb82006-09-24 17:59:33 +000088
admind125c5c2006-09-25 23:26:12 +000089
90<h2>$this->db->last_query();</h2>
adminfb28bb82006-09-24 17:59:33 +000091<p>Returns the last query that was run (the query string, not the result). Example:</p>
92
93<code>$str = $this->db->last_query();<br />
94<br />
95// Produces: SELECT * FROM sometable....
96</code>
97
98
99<p>The following two functions help simplify the process of writing database INSERTs and UPDATEs.</p>
100
101
102<h2>$this->db->insert_string(); </h2>
103<p>This function simplifies the process of writing database inserts. It returns a correctly formatted SQL insert string. Example:</p>
104
105<code>$data = array('name' => $name, 'email' => $email, 'url' => $url);<br />
106<br />
107$str = $this->db->insert_string('table_name', $data);
108</code>
109
110<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>
111<code>INSERT INTO table_name (name, email, url) VALUES ('Rick', 'rick@your-site.com', 'www.your-site.com')</code>
112
113
114
115<h2>$this->db->update_string(); </h2>
116<p>This function simplifies the process of writing database updates. It returns a correctly formatted SQL update string. Example:</p>
117
118<code>$data = array('name' => $name, 'email' => $email, 'url' => $url);<br />
119<br />
120$where = "author_id = 1 AND status = 'active'";
121<br /><br />
122$str = $this->db->update_string('table_name', $data, $where);
123</code>
124
125<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>
126<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>
127
128
129
130
131</div>
132<!-- END CONTENT -->
133
134
135<div id="footer">
136<p>
137Previous Topic:&nbsp;&nbsp;<a href="results.html">Query Results</a>
138&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
139<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
140<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
141Next Topic:&nbsp;&nbsp;<a href="active_record.html">Active Record Pattern</a>
142<p>
143<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>
144</div>
145
146</body>
147</html>