blob: cdf3f68f5a35679f0d07e058c12031a106991703 [file] [log] [blame]
admin3b60ae42006-09-25 23:26:03 +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
Derek Allard404e35d2007-08-07 01:00:45 +00005<title>CodeIgniter User Guide : Database Utility Class</title>
admin3b60ae42006-09-25 23:26:03 +00006
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
admin17a890d2006-09-27 20:42:42 +000010<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000011<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000012<script type="text/javascript" src="../nav/moo.fx.js"></script>
Derek Allardb3412372007-10-25 12:15:16 +000013<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
admin3b60ae42006-09-25 23:26:03 +000014
15<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
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='Rick Ellis' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000020<meta name='description' content='CodeIgniter User Guide' />
admin3b60ae42006-09-25 23:26:03 +000021
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.jpg" width="153" height="44" 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 Allard39b622d2008-01-16 21:10:09 +000031<td><h1>CodeIgniter User Guide Version 1.6.0</h1></td>
adminc0d5d522006-10-30 19:40:35 +000032<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
admin3b60ae42006-09-25 23:26:03 +000033</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">
Derek Allardd2df9bc2007-04-15 17:41:17 +000043<a href="http://www.codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
admin3b60ae42006-09-25 23:26:03 +000044<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45<a href="index.html">Database Library</a> &nbsp;&#8250;&nbsp;
admin24dd7e72006-10-01 19:02:29 +000046Database Utility Class
admin3b60ae42006-09-25 23:26:03 +000047</td>
Derek Allardbc030912007-06-24 18:25:29 +000048<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>
admin3b60ae42006-09-25 23:26:03 +000049</tr>
50</table>
51<!-- END BREADCRUMB -->
52
53
54<br clear="all" />
55
56
57<!-- START CONTENT -->
58<div id="content">
59
admin24dd7e72006-10-01 19:02:29 +000060<h1>Database Utility Class</h1>
admin3b60ae42006-09-25 23:26:03 +000061
admin24dd7e72006-10-01 19:02:29 +000062<p>The Database Utility Class contains functions that help you manage your database.</p>
admin3b60ae42006-09-25 23:26:03 +000063
admin32cb0e52006-09-30 20:36:59 +000064<h3>Table of Contents</h3>
65
66<ul>
admin24dd7e72006-10-01 19:02:29 +000067<li><a href="#init">Initializing the Utility Class</a></li>
admin32cb0e52006-09-30 20:36:59 +000068<li><a href="#list">Listing your Databases</a></li>
69<li><a href="#opttb">Optimizing your Tables</a></li>
70<li><a href="#repair">Repairing your Databases</a></li>
71<li><a href="#optdb">Optimizing your Database</a></li>
admin24dd7e72006-10-01 19:02:29 +000072<li><a href="#csv">CSV Files from a Database Result</a></li>
admin32cb0e52006-09-30 20:36:59 +000073<li><a href="#xml">XML Files from a Database Result</a></li>
74<li><a href="#backup">Backing up your Database</a></li>
75</ul>
76
77
Derek Allard39b622d2008-01-16 21:10:09 +000078
79<h2><a name="init"></a>Initializing the Utility Class</h2>
admin32cb0e52006-09-30 20:36:59 +000080
adminfd2750b2006-10-06 17:29:12 +000081<p class="important"><strong>Important:</strong>&nbsp; In order to initialize the Utility class, your database driver must
admin572ed812006-10-12 18:01:52 +000082already be running, since the utilities class relies on it.</p>
admin3b60ae42006-09-25 23:26:03 +000083
adminfd2750b2006-10-06 17:29:12 +000084<p>Load the Utility Class as follows:</p>
admin3b60ae42006-09-25 23:26:03 +000085
admin572ed812006-10-12 18:01:52 +000086<code>$this->load->dbutil()</code>
admin3b60ae42006-09-25 23:26:03 +000087
88<p>Once initialized you will access the functions using the <dfn>$this->dbutil</dfn> object:</p>
89
90<code>$this->dbutil->some_function()</code>
91
Derek Allard39b622d2008-01-16 21:10:09 +000092<h2><a name="list"></a>$this->dbutil->list_databases()</h2>
admin3b60ae42006-09-25 23:26:03 +000093<p>Returns an array of database names:</p>
94
admin3b60ae42006-09-25 23:26:03 +000095<code>
96$dbs = $this->dbutil->list_databases();<br />
97<br />
98foreach($dbs as $db)<br />
99{<br />
100&nbsp;&nbsp;&nbsp; echo $db;<br />
101}</code>
Derek Allard39b622d2008-01-16 21:10:09 +0000102<h2><a name="opttb"></a>$this->dbutil->optimize_table('table_name');</h2>
admin3b60ae42006-09-25 23:26:03 +0000103
adminf66de862006-10-10 23:30:04 +0000104<p class="important"><strong>Note:</strong>&nbsp; This features is only available for MySQL/MySQLi databases.</p>
105
106
admin3b60ae42006-09-25 23:26:03 +0000107<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>
108
109<code>
110if ($this->dbutil->optimize_table('table_name'))<br />
111{<br />
adminf66de862006-10-10 23:30:04 +0000112&nbsp;&nbsp;&nbsp; echo 'Success!';<br />
admin3b60ae42006-09-25 23:26:03 +0000113}
114</code>
115
116<p><strong>Note:</strong> Not all database platforms support table optimization.</p>
117
118
Derek Allard39b622d2008-01-16 21:10:09 +0000119<h2><a name="repair"></a>$this->dbutil->repair_table('table_name');</h2>
admin3b60ae42006-09-25 23:26:03 +0000120
adminf66de862006-10-10 23:30:04 +0000121<p class="important"><strong>Note:</strong>&nbsp; This features is only available for MySQL/MySQLi databases.</p>
122
123
admin3b60ae42006-09-25 23:26:03 +0000124<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>
125
126<code>
admin985a6382006-10-28 18:47:16 +0000127if ($this->dbutil->repair_table('table_name'))<br />
admin3b60ae42006-09-25 23:26:03 +0000128{<br />
adminf66de862006-10-10 23:30:04 +0000129&nbsp;&nbsp;&nbsp; echo 'Success!';<br />
admin3b60ae42006-09-25 23:26:03 +0000130}
131</code>
132
133<p><strong>Note:</strong> Not all database platforms support table repairs.</p>
134
135
Derek Allard39b622d2008-01-16 21:10:09 +0000136<h2><a name="optdb"></a>$this->dbutil->optimize_database();</h2>
admine5bb9362006-09-27 00:31:22 +0000137
adminf66de862006-10-10 23:30:04 +0000138<p class="important"><strong>Note:</strong>&nbsp; This features is only available for MySQL/MySQLi databases.</p>
139
admin985a6382006-10-28 18:47:16 +0000140<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>
admine5bb9362006-09-27 00:31:22 +0000141
142<code>
adminf66de862006-10-10 23:30:04 +0000143$result = $this->dbutil->optimize_database();<br />
admine5bb9362006-09-27 00:31:22 +0000144<br />
145if ($result !== FALSE)<br />
146{<br />
147&nbsp;&nbsp;&nbsp; print_r($result);<br />
148}
149</code>
150
151<p><strong>Note:</strong> Not all database platforms support table optimization.</p>
admin3b60ae42006-09-25 23:26:03 +0000152
153
Derek Allard39b622d2008-01-16 21:10:09 +0000154<h2><a name="csv"></a>$this->dbutil->csv_from_result($db_result)</h2>
admin3ed8c512006-09-29 23:26:28 +0000155
admine334c472006-10-21 19:44:22 +0000156<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.
admin3ed8c512006-09-29 23:26:28 +0000157Example:</p>
158
159<code>
160$this->load->dbutil();<br />
161<br />
162$query = $this->db->query("SELECT * FROM mytable");<br />
163<br />
admin24dd7e72006-10-01 19:02:29 +0000164echo $this->dbutil->csv_from_result($query);
admin3ed8c512006-09-29 23:26:28 +0000165</code>
166
admine334c472006-10-21 19:44:22 +0000167<p>The second and third parameters allows you to
Derek Allardc6441282007-07-04 23:54:32 +0000168set the delimiter and newline character. By default tabs are used as the delimiter and "\n" is used as a new line. Example:</p>
admin3ed8c512006-09-29 23:26:28 +0000169
170<code>
171$delimiter = ",";<br />
172$newline = "\r\n";<br />
173<br />
admin24dd7e72006-10-01 19:02:29 +0000174echo $this->dbutil->csv_from_result($query, $delimiter, $newline);
admin3ed8c512006-09-29 23:26:28 +0000175</code>
176
adminf66de862006-10-10 23:30:04 +0000177<p><strong>Important:</strong>&nbsp; This function will NOT write the CSV file for you. It simply creates the CSV layout.
admin3ed8c512006-09-29 23:26:28 +0000178If you need to write the file use the <a href="../helpers/file_helper.html">File Helper</a>.</p>
179
180
Derek Allard39b622d2008-01-16 21:10:09 +0000181<h2><a name="xml"></a>$this->dbutil->xml_from_result($db_result)</h2>
admin3ed8c512006-09-29 23:26:28 +0000182
admine334c472006-10-21 19:44:22 +0000183<p>Permits you to generate an XML file from a query result. The first parameter expects a query result object, the second
admin3ed8c512006-09-29 23:26:28 +0000184may contain an optional array of config parameters. Example:</p>
185
186<code>
187$this->load->dbutil();<br />
188<br />
189$query = $this->db->query("SELECT * FROM mytable");<br />
190<br />
191$config = array (<br />
192&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'root'&nbsp;&nbsp;&nbsp; => 'root',<br />
193&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'element' => 'element', <br />
194&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'newline' => "\n", <br />
adminc5f7fa32006-10-06 02:10:23 +0000195&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 />
admin3ed8c512006-09-29 23:26:28 +0000196&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
197<br />
admin24dd7e72006-10-01 19:02:29 +0000198echo $this->dbutil->xml_from_result($query, $config);
admin3ed8c512006-09-29 23:26:28 +0000199</code>
200
adminf66de862006-10-10 23:30:04 +0000201<p><strong>Important:</strong>&nbsp; This function will NOT write the XML file for you. It simply creates the XML layout.
admin3ed8c512006-09-29 23:26:28 +0000202If you need to write the file use the <a href="../helpers/file_helper.html">File Helper</a>.</p>
203
204
Derek Allard39b622d2008-01-16 21:10:09 +0000205<h2><a name="backup"></a>$this->dbutil->backup()</h2>
admin3dd978f2006-09-30 19:24:45 +0000206
admine856ff42006-10-02 02:38:30 +0000207<p>Permits you to backup your full database or individual tables. The backup data can be compressed in either Zip or Gzip format.</p>
admin0e425542006-10-01 03:37:43 +0000208
adminf66de862006-10-10 23:30:04 +0000209<p class="important"><strong>Note:</strong>&nbsp; This features is only available for MySQL/MySQLi databases.</p>
210
admin0e425542006-10-01 03:37:43 +0000211<p>Note: Due to the limited execution time and memory available to PHP, backing up very large
admine334c472006-10-21 19:44:22 +0000212databases may not be possible. If your database is very large you might need to backup directly from your SQL server
admin0e425542006-10-01 03:37:43 +0000213via the command line, or have your server admin do it for you if you do not have root privileges.</p>
214
admine856ff42006-10-02 02:38:30 +0000215<h3>Usage Example</h3>
admin3ed8c512006-09-29 23:26:28 +0000216
admine856ff42006-10-02 02:38:30 +0000217<code>
218<dfn>// Load the DB utility class</dfn><br />
219$this->load->dbutil();<br /><br />
220
adminc5f7fa32006-10-06 02:10:23 +0000221<dfn>// Backup your entire database and assign it to a variable</dfn><br />
admine856ff42006-10-02 02:38:30 +0000222$backup =& $this->dbutil->backup();
223
224<br /><br />
225<dfn>// Load the file helper and write the file to your server</dfn><br />
226$this->load->helper('file');<br />
227write_file('/path/to/mybackup.gz', $backup);
228
229<br /><br />
230<dfn>// Load the download helper and send the file to your desktop</dfn><br />
231$this->load->helper('download');<br />
232force_download('mybackup.gz', $backup);
admin32cb0e52006-09-30 20:36:59 +0000233</code>
admin3ed8c512006-09-29 23:26:28 +0000234
admin32cb0e52006-09-30 20:36:59 +0000235<h3>Setting Backup Preferences</h3>
236
admine856ff42006-10-02 02:38:30 +0000237<p>Backup preferences are set by submitting an array of values to the first parameter of the backup function. Example:</p>
admin32cb0e52006-09-30 20:36:59 +0000238
239<code>$prefs = array(<br />
240&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 />
admine856ff42006-10-02 02:38:30 +0000241&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 />
242&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 />
243&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 />
244&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 />
245&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 />
246&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 />
admin32cb0e52006-09-30 20:36:59 +0000247&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
248<br />
249$this->dbutil->backup($prefs);
250</code>
251
admin0e425542006-10-01 03:37:43 +0000252
admin32cb0e52006-09-30 20:36:59 +0000253<h3>Description of Backup Preferences</h3>
254
255<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
256<tr>
257<th>Preference</th>
258<th>Default&nbsp;Value</th>
259<th>Options</th>
260<th>Description</th>
261</tr><tr>
admine856ff42006-10-02 02:38:30 +0000262<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>
admin32cb0e52006-09-30 20:36:59 +0000263</tr><tr>
admine856ff42006-10-02 02:38:30 +0000264<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>
admin32cb0e52006-09-30 20:36:59 +0000265</tr><tr>
266<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>
267</tr><tr>
admine856ff42006-10-02 02:38:30 +0000268<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>
admin32cb0e52006-09-30 20:36:59 +0000269</tr><tr>
270<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>
271</tr><tr>
admin40037182006-10-11 19:16:58 +0000272<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>
admin32cb0e52006-09-30 20:36:59 +0000273</tr><tr>
admin40037182006-10-11 19:16:58 +0000274<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>
admin32cb0e52006-09-30 20:36:59 +0000275
276</tr>
277</table>
278
admin3b60ae42006-09-25 23:26:03 +0000279
280</div>
281<!-- END CONTENT -->
282
283
284<div id="footer">
285<p>
Derek Allard39b622d2008-01-16 21:10:09 +0000286Previous Topic:&nbsp;&nbsp;<a href="forge.html">DB Forge Class</a>
admin3b60ae42006-09-25 23:26:03 +0000287&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
288<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
289<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Derek Allard39b622d2008-01-16 21:10:09 +0000290Next Topic:&nbsp;&nbsp;<a href="../libraries/email.html"> Email Class</a></p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000291<p><a href="http://www.codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2007 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
admin3b60ae42006-09-25 23:26:03 +0000292</div>
293
294</body>
295</html>