Remove file execution permission.
[living-lab-site.git] / user_guide / database / helpers.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3 <head>
4
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6 <title>Query Helper Functions : CodeIgniter User Guide</title>
7
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
11 <script type="text/javascript" src="../nav/nav.js"></script>
12 <script type="text/javascript" src="../nav/prototype.lite.js"></script>
13 <script type="text/javascript" src="../nav/moo.fx.js"></script>
14 <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
15
16 <meta http-equiv='expires' content='-1' />
17 <meta http-equiv= 'pragma' content='no-cache' />
18 <meta name='robots' content='all' />
19 <meta name='author' content='ExpressionEngine Dev Team' />
20 <meta name='description' content='CodeIgniter User Guide' />
21
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_darker.jpg" width="154" height="43" 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>
31 <td><h1>CodeIgniter User Guide Version 2.0.2</h1></td>
32 <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
33 </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">
43 <a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
44 <a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45 <a href="index.html">Database Library</a> &nbsp;&#8250;&nbsp;
46 Query Helpers
47 </td>
48 <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>
49 </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>
70 <p>Note:  In MySQL "DELETE FROM TABLE" returns 0 affected rows. The database class has a small hack that allows it to return the
71 correct number of affected rows.  By default this hack is enabled but it can be turned off in the database driver file.</p>
72
73
74 <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
82 <h2>$this->db->platform()</h2>
83 <p>Outputs the database platform you are running (MySQL, MS SQL, Postgres, etc...):</p>
84 <code>echo $this->db->platform();</code>
85
86
87 <h2>$this->db->version()</h2>
88 <p>Outputs the database version you are running:</p>
89 <code>echo $this->db->version();</code>
90
91
92 <h2>$this->db->last_query();</h2>
93 <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>
113 <code>INSERT INTO table_name (name, email, url) VALUES ('Rick', 'rick@example.com', 'example.com')</code>
114
115 <p class="important">Note: Values are automatically escaped, producing safer queries.</p>
116
117
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
129 <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>
130 <code> UPDATE table_name SET name = 'Rick', email = 'rick@example.com', url = 'example.com' WHERE author_id = 1 AND status = 'active'</code>
131
132 <p class="important">Note: Values are automatically escaped, producing safer queries.</p>
133
134
135 </div>
136 <!-- END CONTENT -->
137
138
139 <div id="footer">
140 <p>
141 Previous 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;
145 Next Topic:&nbsp;&nbsp;<a href="active_record.html">Active Record Pattern</a>
146 </p>
147 <p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 - 2011 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
148 </div>
149
150 </body>
151 </html>