blob: a5151e35ea4c1288f63e2a3d3404c27baa0523a2 [file] [log] [blame]
admin6bc01412006-09-24 03:03:51 +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>$query->num_rows()</h2>
71<p>The number of rows returned by the query. Note: In this example, <dfn>$query</dfn> is the variable that the query result object is assigned to:</p>
72
73<code>$query = $this->db->query('SELECT * FROM my_table');<br /><br />
74echo $query->num_rows();
75</code>
76
77<h2>$query->num_fields()</h2>
78<p>The number of FIELDS returned by the query. Make sure to call the function using your query result object:</p>
79
80<code>$query = $this->db->query('SELECT * FROM my_table');<br /><br />
81echo $query->num_fields();
82</code>
83
84
85<h2>$this->db->insert_id()</h2>
86<p>The insert ID number when performing database inserts.</p>
87
88<h2>$this->db->affected_rows()</h2>
89<p>Displays the number of affected rows, when doing "write" type queries (insert, update, etc.).</p>
90</p>Note: In MySQL "DELETE FROM TABLE" returns 0 affected rows. The database class has a small hack that allows it to return the
91correct number of affected rows. By default this hack is enabled but it can be turned off in the database driver file.</p>
92
93
94<h2>$this->db->version()</h2>
95<p>Outputs the database version you are running:</p>
96
97<code>echo $this->db->version();</code>
98
99<h2>$this->db->last_query();</h2>
100
101<p>Returns the last query that was run (the query string, not the result). Example:</p>
102
103<code>$str = $this->db->last_query();<br />
104<br />
105// Produces: SELECT * FROM sometable....
106</code>
107
108
109<p>The following two functions help simplify the process of writing database INSERTs and UPDATEs.</p>
110
111
112<h2>$this->db->insert_string(); </h2>
113<p>This function simplifies the process of writing database inserts. It returns a correctly formatted SQL insert string. Example:</p>
114
115<code>$data = array('name' => $name, 'email' => $email, 'url' => $url);<br />
116<br />
117$str = $this->db->insert_string('table_name', $data);
118</code>
119
120<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>
121<code>INSERT INTO table_name (name, email, url) VALUES ('Rick', 'rick@your-site.com', 'www.your-site.com')</code>
122
123
124
125<h2>$this->db->update_string(); </h2>
126<p>This function simplifies the process of writing database updates. It returns a correctly formatted SQL update string. Example:</p>
127
128<code>$data = array('name' => $name, 'email' => $email, 'url' => $url);<br />
129<br />
130$where = "author_id = 1 AND status = 'active'";
131<br /><br />
132$str = $this->db->update_string('table_name', $data, $where);
133</code>
134
135<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>
136<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>
137
138
139
140
141</div>
142<!-- END CONTENT -->
143
144
145<div id="footer">
146<p>
147Previous Topic:&nbsp;&nbsp;<a href="connecting.html">Connecting to your Database</a>
148&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
149<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
150<a href="../../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
151Next Topic:&nbsp;&nbsp;<a href="results.html">Query Results</a>
152<p>
153<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>
154</div>
155
156</body>
157</html>