blob: c80e3d1066a873c99024e156025a1a7f30faf86f [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
Kyle Farris8c50c8e2011-09-09 12:11:21 -0300186<p>The second, third, and fourth parameters allow you to
Kyle Farris07f8fb32011-09-09 12:15:52 -0300187set the delimiter, newline, and enclosure characters respectively. By default tabs are used as the delimiter, "\n" is used as a new line, and a double-quote is used as the enclosure. Example:</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000188
189<code>
190$delimiter = ",";<br />
191$newline = "\r\n";<br />
Kyle Farris8c50c8e2011-09-09 12:11:21 -0300192$enclosure = '"';<br />
Derek Allard2067d1a2008-11-13 22:59:24 +0000193<br />
Kyle Farris8c50c8e2011-09-09 12:11:21 -0300194echo $this->dbutil->csv_from_result($query, $delimiter, $newline, $enclosure);
Derek Allard2067d1a2008-11-13 22:59:24 +0000195</code>
196
Derek Jones4b9c6292011-07-01 17:40:48 -0500197<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 +0000198If you need to write the file use the <a href="../helpers/file_helper.html">File Helper</a>.</p>
199
200
201<h2><a name="xml"></a>$this->dbutil->xml_from_result($db_result)</h2>
202
203<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 -0500204may contain an optional array of config parameters. Example:</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000205
206<code>
207$this->load->dbutil();<br />
208<br />
209$query = $this->db->query("SELECT * FROM mytable");<br />
210<br />
211$config = array (<br />
212&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'root'&nbsp;&nbsp;&nbsp; => 'root',<br />
213&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'element' => 'element', <br />
214&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'newline' => "\n", <br />
215&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 />
216&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
217<br />
218echo $this->dbutil->xml_from_result($query, $config);
219</code>
220
Derek Jones4b9c6292011-07-01 17:40:48 -0500221<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 +0000222If you need to write the file use the <a href="../helpers/file_helper.html">File Helper</a>.</p>
223
224
225<h2><a name="backup"></a>$this->dbutil->backup()</h2>
226
Derek Jones4b9c6292011-07-01 17:40:48 -0500227<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 +0000228
229<p class="important"><strong>Note:</strong>&nbsp; This features is only available for MySQL databases.</p>
230
231<p>Note: Due to the limited execution time and memory available to PHP, backing up very large
Derek Jones4b9c6292011-07-01 17:40:48 -0500232databases 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 +0000233via the command line, or have your server admin do it for you if you do not have root privileges.</p>
234
235<h3>Usage Example</h3>
236
237<code>
238<dfn>// Load the DB utility class</dfn><br />
239$this->load->dbutil();<br /><br />
240
241<dfn>// Backup your entire database and assign it to a variable</dfn><br />
242$backup =& $this->dbutil->backup();
243
244<br /><br />
245<dfn>// Load the file helper and write the file to your server</dfn><br />
246$this->load->helper('file');<br />
247write_file('/path/to/mybackup.gz', $backup);
248
249<br /><br />
250<dfn>// Load the download helper and send the file to your desktop</dfn><br />
251$this->load->helper('download');<br />
252force_download('mybackup.gz', $backup);
253</code>
254
255<h3>Setting Backup Preferences</h3>
256
257<p>Backup preferences are set by submitting an array of values to the first parameter of the backup function. Example:</p>
258
259<code>$prefs = array(<br />
260&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 />
261&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 />
262&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 />
263&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 />
264&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 />
265&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 />
266&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 />
267&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
268<br />
269$this->dbutil->backup($prefs);
270</code>
271
272
273<h3>Description of Backup Preferences</h3>
274
275<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
276<tr>
277<th>Preference</th>
278<th>Default&nbsp;Value</th>
279<th>Options</th>
280<th>Description</th>
281</tr><tr>
Derek Jones4b9c6292011-07-01 17:40:48 -0500282<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 +0000283</tr><tr>
284<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>
285</tr><tr>
286<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>
287</tr><tr>
288<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>
289</tr><tr>
290<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>
291</tr><tr>
292<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>
293</tr><tr>
294<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>
295
296</tr>
297</table>
298
299
300</div>
301<!-- END CONTENT -->
302
303
304<div id="footer">
305<p>
306Previous Topic:&nbsp;&nbsp;<a href="forge.html">DB Forge Class</a>
307&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
308<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
309<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
kenjis55e9d6b2011-04-15 11:34:53 +0900310Next Topic:&nbsp;&nbsp;<a href="../libraries/javascript.html">Javascript Class</a></p>
Derek Jones700205a2011-01-28 07:44:28 -0600311<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 +0000312</div>
313
314</body>
admin3b60ae42006-09-25 23:26:03 +0000315</html>