blob: 8231c7e78a9ccd3f87d9a7ee262cad6308781bcb [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<!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>Database Utility Class : 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>
Derek Jonesb8c038a2011-08-20 08:57:14 -050031<td><h1>CodeIgniter User Guide Version 2.0.3</h1></td>
Derek Allard2067d1a2008-11-13 22:59:24 +000032<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;
46Database Utility Class
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<br clear="all" />
55
56
57<!-- START CONTENT -->
58<div id="content">
59
60<h1>Database Utility Class</h1>
61
62<p>The Database Utility Class contains functions that help you manage your database.</p>
63
64<h3>Table of Contents</h3>
65
66<ul>
67<li><a href="#init">Initializing the Utility Class</a></li>
68<li><a href="#list">Listing your Databases</a></li>
Derek Allarde7f03252010-02-04 16:47:01 +000069<li><a href="#exists">Checking for a specific Database</a></li>
Derek Allard2067d1a2008-11-13 22:59:24 +000070<li><a href="#opttb">Optimizing your Tables</a></li>
71<li><a href="#repair">Repairing your Databases</a></li>
72<li><a href="#optdb">Optimizing your Database</a></li>
73<li><a href="#csv">CSV Files from a Database Result</a></li>
74<li><a href="#xml">XML Files from a Database Result</a></li>
75<li><a href="#backup">Backing up your Database</a></li>
76</ul>
77
78
79
80<h2><a name="init"></a>Initializing the Utility Class</h2>
81
82<p class="important"><strong>Important:</strong>&nbsp; In order to initialize the Utility class, your database driver must
83already be running, since the utilities class relies on it.</p>
84
85<p>Load the Utility Class as follows:</p>
86
87<code>$this->load->dbutil()</code>
88
89<p>Once initialized you will access the functions using the <dfn>$this->dbutil</dfn> object:</p>
90
91<code>$this->dbutil->some_function()</code>
92
93<h2><a name="list"></a>$this->dbutil->list_databases()</h2>
94<p>Returns an array of database names:</p>
95
96<code>
97$dbs = $this->dbutil->list_databases();<br />
98<br />
Pascal Krietefeba2de2011-02-14 13:25:30 -050099foreach ($dbs as $db)<br />
Derek Allard2067d1a2008-11-13 22:59:24 +0000100{<br />
101&nbsp;&nbsp;&nbsp; echo $db;<br />
102}</code>
Derek Allarde7f03252010-02-04 16:47:01 +0000103
104
Eric Barnesc7f2bd22011-01-30 20:50:08 -0500105<h2><a name="exists"></a>$this->dbutil->database_exists();</h2>
Derek Allarde7f03252010-02-04 16:47:01 +0000106
107<p>Sometimes it's helpful to know whether a particular database exists.
Derek Jones4b9c6292011-07-01 17:40:48 -0500108Returns a boolean TRUE/FALSE. Usage example:</p>
Derek Allarde7f03252010-02-04 16:47:01 +0000109
110<code>
Eric Barnesc7f2bd22011-01-30 20:50:08 -0500111if ($this->dbutil->database_exists('database_name'))<br />
Derek Allarde7f03252010-02-04 16:47:01 +0000112{<br />
113&nbsp;&nbsp; // some code...<br />
114}
115</code>
116
Derek Jones4b9c6292011-07-01 17:40:48 -0500117<p>Note: Replace <em>database_name</em> with the name of the table you are looking for. This function is case sensitive.</p>
Derek Allarde7f03252010-02-04 16:47:01 +0000118
119
120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121<h2><a name="opttb"></a>$this->dbutil->optimize_table('table_name');</h2>
122
123<p class="important"><strong>Note:</strong>&nbsp; This features is only available for MySQL/MySQLi databases.</p>
124
125
126<p>Permits you to optimize a table using the table name specified in the first parameter. Returns TRUE/FALSE based on success or failure:</p>
127
128<code>
129if ($this->dbutil->optimize_table('table_name'))<br />
130{<br />
131&nbsp;&nbsp;&nbsp; echo 'Success!';<br />
132}
133</code>
134
135<p><strong>Note:</strong> Not all database platforms support table optimization.</p>
136
137
138<h2><a name="repair"></a>$this->dbutil->repair_table('table_name');</h2>
139
140<p class="important"><strong>Note:</strong>&nbsp; This features is only available for MySQL/MySQLi databases.</p>
141
142
143<p>Permits you to repair a table using the table name specified in the first parameter. Returns TRUE/FALSE based on success or failure:</p>
144
145<code>
146if ($this->dbutil->repair_table('table_name'))<br />
147{<br />
148&nbsp;&nbsp;&nbsp; echo 'Success!';<br />
149}
150</code>
151
152<p><strong>Note:</strong> Not all database platforms support table repairs.</p>
153
154
155<h2><a name="optdb"></a>$this->dbutil->optimize_database();</h2>
156
157<p class="important"><strong>Note:</strong>&nbsp; This features is only available for MySQL/MySQLi databases.</p>
158
159<p>Permits you to optimize the database your DB class is currently connected to. Returns an array containing the DB status messages or FALSE on failure.</p>
160
161<code>
162$result = $this->dbutil->optimize_database();<br />
163<br />
164if ($result !== FALSE)<br />
165{<br />
166&nbsp;&nbsp;&nbsp; print_r($result);<br />
167}
168</code>
169
170<p><strong>Note:</strong> Not all database platforms support table optimization.</p>
171
172
173<h2><a name="csv"></a>$this->dbutil->csv_from_result($db_result)</h2>
174
175<p>Permits you to generate a CSV file from a query result. The first parameter of the function must contain the result object from your query.
176Example:</p>
177
178<code>
179$this->load->dbutil();<br />
180<br />
181$query = $this->db->query("SELECT * FROM mytable");<br />
182<br />
183echo $this->dbutil->csv_from_result($query);
184</code>
185
186<p>The second and third parameters allows you to
Derek Jones4b9c6292011-07-01 17:40:48 -0500187set the delimiter and newline character. By default tabs are used as the delimiter and "\n" is used as a new line. Example:</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000188
189<code>
190$delimiter = ",";<br />
191$newline = "\r\n";<br />
192<br />
193echo $this->dbutil->csv_from_result($query, $delimiter, $newline);
194</code>
195
Derek Jones4b9c6292011-07-01 17:40:48 -0500196<p><strong>Important:</strong>&nbsp; This function will NOT write the CSV file for you. It simply creates the CSV layout.
Derek Allard2067d1a2008-11-13 22:59:24 +0000197If you need to write the file use the <a href="../helpers/file_helper.html">File Helper</a>.</p>
198
199
200<h2><a name="xml"></a>$this->dbutil->xml_from_result($db_result)</h2>
201
202<p>Permits you to generate an XML file from a query result. The first parameter expects a query result object, the second
Derek Jones4b9c6292011-07-01 17:40:48 -0500203may contain an optional array of config parameters. Example:</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000204
205<code>
206$this->load->dbutil();<br />
207<br />
208$query = $this->db->query("SELECT * FROM mytable");<br />
209<br />
210$config = array (<br />
211&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'root'&nbsp;&nbsp;&nbsp; => 'root',<br />
212&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'element' => 'element', <br />
213&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'newline' => "\n", <br />
214&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'tab'&nbsp;&nbsp;&nbsp;&nbsp;=> "\t"<br />
215&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
216<br />
217echo $this->dbutil->xml_from_result($query, $config);
218</code>
219
Derek Jones4b9c6292011-07-01 17:40:48 -0500220<p><strong>Important:</strong>&nbsp; This function will NOT write the XML file for you. It simply creates the XML layout.
Derek Allard2067d1a2008-11-13 22:59:24 +0000221If you need to write the file use the <a href="../helpers/file_helper.html">File Helper</a>.</p>
222
223
224<h2><a name="backup"></a>$this->dbutil->backup()</h2>
225
Derek Jones4b9c6292011-07-01 17:40:48 -0500226<p>Permits you to backup your full database or individual tables. The backup data can be compressed in either Zip or Gzip format.</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000227
228<p class="important"><strong>Note:</strong>&nbsp; This features is only available for MySQL databases.</p>
229
230<p>Note: Due to the limited execution time and memory available to PHP, backing up very large
Derek Jones4b9c6292011-07-01 17:40:48 -0500231databases may not be possible. If your database is very large you might need to backup directly from your SQL server
Derek Allard2067d1a2008-11-13 22:59:24 +0000232via the command line, or have your server admin do it for you if you do not have root privileges.</p>
233
234<h3>Usage Example</h3>
235
236<code>
237<dfn>// Load the DB utility class</dfn><br />
238$this->load->dbutil();<br /><br />
239
240<dfn>// Backup your entire database and assign it to a variable</dfn><br />
241$backup =& $this->dbutil->backup();
242
243<br /><br />
244<dfn>// Load the file helper and write the file to your server</dfn><br />
245$this->load->helper('file');<br />
246write_file('/path/to/mybackup.gz', $backup);
247
248<br /><br />
249<dfn>// Load the download helper and send the file to your desktop</dfn><br />
250$this->load->helper('download');<br />
251force_download('mybackup.gz', $backup);
252</code>
253
254<h3>Setting Backup Preferences</h3>
255
256<p>Backup preferences are set by submitting an array of values to the first parameter of the backup function. Example:</p>
257
258<code>$prefs = array(<br />
259&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'tables'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> array('table1', 'table2'),&nbsp;&nbsp;// Array of tables to backup.<br />
260&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'ignore'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> array(),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// List of tables to omit from the backup<br />
261&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'format'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> 'txt',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// gzip, zip, txt<br />
262&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'filename'&nbsp;&nbsp;&nbsp;&nbsp;=> 'mybackup.sql',&nbsp;&nbsp;&nbsp;&nbsp;// File name - NEEDED ONLY WITH ZIP FILES<br />
263&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'add_drop'&nbsp;&nbsp;&nbsp;&nbsp;=> TRUE,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Whether to add DROP TABLE statements to backup file<br />
264&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'add_insert'&nbsp;&nbsp;=> TRUE,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Whether to add INSERT data to backup file<br />
265&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'newline'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> "\n"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Newline character used in backup file<br />
266&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
267<br />
268$this->dbutil->backup($prefs);
269</code>
270
271
272<h3>Description of Backup Preferences</h3>
273
274<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
275<tr>
276<th>Preference</th>
277<th>Default&nbsp;Value</th>
278<th>Options</th>
279<th>Description</th>
280</tr><tr>
Derek Jones4b9c6292011-07-01 17:40:48 -0500281<td class="td"><strong>tables</strong></td><td class="td">empty array</td><td class="td">None</td><td class="td">An array of tables you want backed up. If left blank all tables will be exported.</td>
Derek Allard2067d1a2008-11-13 22:59:24 +0000282</tr><tr>
283<td class="td"><strong>ignore</strong></td><td class="td">empty array</td><td class="td">None</td><td class="td">An array of tables you want the backup routine to ignore.</td>
284</tr><tr>
285<td class="td"><strong>format</strong></td><td class="td">gzip</td><td class="td">gzip, zip, txt</td><td class="td">The file format of the export file.</td>
286</tr><tr>
287<td class="td"><strong>filename</strong></td><td class="td">the current date/time</td><td class="td">None</td><td class="td">The name of the backed-up file. The name is needed only if you are using zip compression.</td>
288</tr><tr>
289<td class="td"><strong>add_drop</strong></td><td class="td">TRUE</td><td class="td">TRUE/FALSE</td><td class="td">Whether to include DROP TABLE statements in your SQL export file.</td>
290</tr><tr>
291<td class="td"><strong>add_insert</strong></td><td class="td">TRUE</td><td class="td">TRUE/FALSE</td><td class="td">Whether to include INSERT statements in your SQL export file.</td>
292</tr><tr>
293<td class="td"><strong>newline</strong></td><td class="td">"\n"</td><td class="td">"\n", "\r", "\r\n"</td><td class="td">Type of newline to use in your SQL export file.</td>
294
295</tr>
296</table>
297
298
299</div>
300<!-- END CONTENT -->
301
302
303<div id="footer">
304<p>
305Previous Topic:&nbsp;&nbsp;<a href="forge.html">DB Forge Class</a>
306&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
307<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
308<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
kenjis55e9d6b2011-04-15 11:34:53 +0900309Next Topic:&nbsp;&nbsp;<a href="../libraries/javascript.html">Javascript Class</a></p>
Derek Jones700205a2011-01-28 07:44:28 -0600310<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>
Derek Allard2067d1a2008-11-13 22:59:24 +0000311</div>
312
313</body>
admin3b60ae42006-09-25 23:26:03 +0000314</html>