blob: f815039cbacb83d367239c0cf02f420ce93ea434 [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
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
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>
admin3b60ae42006-09-25 23:26:03 +000013<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>
admin41a16852006-09-26 18:17:19 +000036<td><h1>Code Igniter User Guide Version 1.5.0</h1></td>
admin3b60ae42006-09-25 23:26:03 +000037<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;
admin24dd7e72006-10-01 19:02:29 +000051Database Utility Class
admin3b60ae42006-09-25 23:26:03 +000052</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<br clear="all" />
60
61
62<!-- START CONTENT -->
63<div id="content">
64
admin24dd7e72006-10-01 19:02:29 +000065<h1>Database Utility Class</h1>
admin3b60ae42006-09-25 23:26:03 +000066
admin24dd7e72006-10-01 19:02:29 +000067<p>The Database Utility Class contains functions that help you manage your database.</p>
admin3b60ae42006-09-25 23:26:03 +000068
admin32cb0e52006-09-30 20:36:59 +000069<h3>Table of Contents</h3>
70
71<ul>
admin24dd7e72006-10-01 19:02:29 +000072<li><a href="#init">Initializing the Utility Class</a></li>
admin32cb0e52006-09-30 20:36:59 +000073<li><a href="#create">Creating a Database</a></li>
74<li><a href="#drop">Dropping a Database</a></li>
75<li><a href="#list">Listing your Databases</a></li>
76<li><a href="#opttb">Optimizing your Tables</a></li>
77<li><a href="#repair">Repairing your Databases</a></li>
78<li><a href="#optdb">Optimizing your Database</a></li>
admin24dd7e72006-10-01 19:02:29 +000079<li><a href="#csv">CSV Files from a Database Result</a></li>
admin32cb0e52006-09-30 20:36:59 +000080<li><a href="#xml">XML Files from a Database Result</a></li>
81<li><a href="#backup">Backing up your Database</a></li>
82</ul>
83
84
85<a name="init"></a>
admin24dd7e72006-10-01 19:02:29 +000086<h2>Initializing the Utility Class</h2>
admin32cb0e52006-09-30 20:36:59 +000087
adminfd2750b2006-10-06 17:29:12 +000088<p class="important"><strong>Important:</strong>&nbsp; In order to initialize the Utility class, your database driver must
89already be running, since the utilities class is loaded from within the main DB class.</p>
admin3b60ae42006-09-25 23:26:03 +000090
adminfd2750b2006-10-06 17:29:12 +000091<p>Load the Utility Class as follows:</p>
admin3b60ae42006-09-25 23:26:03 +000092
adminfd2750b2006-10-06 17:29:12 +000093<code>$this->db->load_utilities()</code>
admin3b60ae42006-09-25 23:26:03 +000094
95<p>Once initialized you will access the functions using the <dfn>$this->dbutil</dfn> object:</p>
96
97<code>$this->dbutil->some_function()</code>
98
99
100
admin32cb0e52006-09-30 20:36:59 +0000101<a name="create"></a>
admin3b60ae42006-09-25 23:26:03 +0000102<h2>$this->dbutil->create_database('db_name')</h2>
103
104<p>Permits you to create a database using the name specified in the first parameter. Returns TRUE/FALSE based on success or failure:</p>
105
106<code>if ($this->dbutil->create_database('my_db'))<br />
107{<br />
108&nbsp;&nbsp;&nbsp; echo 'Database created!';<br />
109}</code>
110
111
admin32cb0e52006-09-30 20:36:59 +0000112
113<a name="drop"></a>
admin3b60ae42006-09-25 23:26:03 +0000114<h2>$this->dbutil->drop_database('table_name')</h2>
115
116<p>Permits you to drop a database using the table name specified in the first parameter. Returns TRUE/FALSE based on success or failure:</p>
117
118<code>if ($this->dbutil->drop_database('my_db'))<br />
119{<br />
120&nbsp;&nbsp;&nbsp; echo 'Database deleted!';<br />
121}</code>
122
123
admin32cb0e52006-09-30 20:36:59 +0000124<a name="list"></a>
admin3b60ae42006-09-25 23:26:03 +0000125<h2>$this->dbutil->list_databases()</h2>
admin3b60ae42006-09-25 23:26:03 +0000126<p>Returns an array of database names:</p>
127
128
129<code>
130$dbs = $this->dbutil->list_databases();<br />
131<br />
132foreach($dbs as $db)<br />
133{<br />
134&nbsp;&nbsp;&nbsp; echo $db;<br />
135}</code>
136
137
admin32cb0e52006-09-30 20:36:59 +0000138<a name="opttb"></a>
admin3b60ae42006-09-25 23:26:03 +0000139<h2>$this->dbutil->optimize_table('table_name');</h2>
140
141<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>
142
143<code>
144if ($this->dbutil->optimize_table('table_name'))<br />
145{<br />
146&nbsp;&nbsp;&nbsp; echo 'Success!'<br />
147}
148</code>
149
150<p><strong>Note:</strong> Not all database platforms support table optimization.</p>
151
152
admin32cb0e52006-09-30 20:36:59 +0000153<a name="repair"></a>
admin3b60ae42006-09-25 23:26:03 +0000154<h2>$this->dbutil->repair_table('table_name');</h2>
155
156<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>
157
158<code>
159if ($this->dbutil->optimize_table('table_name'))<br />
160{<br />
161&nbsp;&nbsp;&nbsp; echo 'Success!'<br />
162}
163</code>
164
165<p><strong>Note:</strong> Not all database platforms support table repairs.</p>
166
167
admin32cb0e52006-09-30 20:36:59 +0000168<a name="optdb"></a>
admine5bb9362006-09-27 00:31:22 +0000169<h2>$this->dbutil->optimize_database();</h2>
170
171<p>Permits you to optimize the database your DB class is currently connected to. Returns an array containing the returned status messages or FALSE on failure.</p>
172
173<code>
admin32cb0e52006-09-30 20:36:59 +0000174$result = $this->dbutil->optimize_databass();<br />
admine5bb9362006-09-27 00:31:22 +0000175<br />
176if ($result !== FALSE)<br />
177{<br />
178&nbsp;&nbsp;&nbsp; print_r($result);<br />
179}
180</code>
181
182<p><strong>Note:</strong> Not all database platforms support table optimization.</p>
admin3b60ae42006-09-25 23:26:03 +0000183
184
admin24dd7e72006-10-01 19:02:29 +0000185<a name="csv"></a>
186<h2>$this->dbutil->csv_from_result($db_result)</h2>
admin3ed8c512006-09-29 23:26:28 +0000187
admin24dd7e72006-10-01 19:02:29 +0000188<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 +0000189Example:</p>
190
191<code>
192$this->load->dbutil();<br />
193<br />
194$query = $this->db->query("SELECT * FROM mytable");<br />
195<br />
admin24dd7e72006-10-01 19:02:29 +0000196echo $this->dbutil->csv_from_result($query);
admin3ed8c512006-09-29 23:26:28 +0000197</code>
198
199<p>The second and third parameters allows you to
200set the delimiter and newline character. By default tabs are used as the delimiter and "\n" is used as a new line. Example:
201
202<code>
203$delimiter = ",";<br />
204$newline = "\r\n";<br />
205<br />
admin24dd7e72006-10-01 19:02:29 +0000206echo $this->dbutil->csv_from_result($query, $delimiter, $newline);
admin3ed8c512006-09-29 23:26:28 +0000207</code>
208
admin24dd7e72006-10-01 19:02:29 +0000209<p class="important"><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 +0000210If you need to write the file use the <a href="../helpers/file_helper.html">File Helper</a>.</p>
211
212
admin32cb0e52006-09-30 20:36:59 +0000213<a name="xml"></a>
admin3ed8c512006-09-29 23:26:28 +0000214<h2>$this->dbutil->xml_from_result($db_result)</h2>
215
216<p>Permits you to generate an XML file from a query result. The first parameter expects a query result object, the second
217may contain an optional array of config parameters. Example:</p>
218
219<code>
220$this->load->dbutil();<br />
221<br />
222$query = $this->db->query("SELECT * FROM mytable");<br />
223<br />
224$config = array (<br />
225&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'root'&nbsp;&nbsp;&nbsp; => 'root',<br />
226&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'element' => 'element', <br />
227&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 +0000228&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 +0000229&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
230<br />
admin24dd7e72006-10-01 19:02:29 +0000231echo $this->dbutil->xml_from_result($query, $config);
admin3ed8c512006-09-29 23:26:28 +0000232</code>
233
admin24dd7e72006-10-01 19:02:29 +0000234<p class="important"><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 +0000235If you need to write the file use the <a href="../helpers/file_helper.html">File Helper</a>.</p>
236
237
admin32cb0e52006-09-30 20:36:59 +0000238<a name="backup"></a>
239<h2>$this->dbutil->backup()</h2>
admin3dd978f2006-09-30 19:24:45 +0000240
admine856ff42006-10-02 02:38:30 +0000241<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 +0000242
243<p>Note: Due to the limited execution time and memory available to PHP, backing up very large
244databases may not be possible. If your database is very large you might need to backup directly from your SQL server
245via the command line, or have your server admin do it for you if you do not have root privileges.</p>
246
admine856ff42006-10-02 02:38:30 +0000247<h3>Usage Example</h3>
admin3ed8c512006-09-29 23:26:28 +0000248
admine856ff42006-10-02 02:38:30 +0000249<code>
250<dfn>// Load the DB utility class</dfn><br />
251$this->load->dbutil();<br /><br />
252
adminc5f7fa32006-10-06 02:10:23 +0000253<dfn>// Backup your entire database and assign it to a variable</dfn><br />
admine856ff42006-10-02 02:38:30 +0000254$backup =& $this->dbutil->backup();
255
256<br /><br />
257<dfn>// Load the file helper and write the file to your server</dfn><br />
258$this->load->helper('file');<br />
259write_file('/path/to/mybackup.gz', $backup);
260
261<br /><br />
262<dfn>// Load the download helper and send the file to your desktop</dfn><br />
263$this->load->helper('download');<br />
264force_download('mybackup.gz', $backup);
admin32cb0e52006-09-30 20:36:59 +0000265</code>
admin3ed8c512006-09-29 23:26:28 +0000266
admin32cb0e52006-09-30 20:36:59 +0000267<h3>Setting Backup Preferences</h3>
268
admine856ff42006-10-02 02:38:30 +0000269<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 +0000270
271<code>$prefs = array(<br />
272&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 +0000273&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 />
274&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 />
275&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 />
276&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 />
277&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 />
278&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 +0000279&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
280<br />
281$this->dbutil->backup($prefs);
282</code>
283
admin0e425542006-10-01 03:37:43 +0000284
admin32cb0e52006-09-30 20:36:59 +0000285<h3>Description of Backup Preferences</h3>
286
287<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
288<tr>
289<th>Preference</th>
290<th>Default&nbsp;Value</th>
291<th>Options</th>
292<th>Description</th>
293</tr><tr>
admine856ff42006-10-02 02:38:30 +0000294<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 +0000295</tr><tr>
admine856ff42006-10-02 02:38:30 +0000296<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 +0000297</tr><tr>
298<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>
299</tr><tr>
admine856ff42006-10-02 02:38:30 +0000300<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 +0000301</tr><tr>
302<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>
303</tr><tr>
304<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>
305</tr><tr>
306<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>
307
308</tr>
309</table>
310
admin3b60ae42006-09-25 23:26:03 +0000311
312</div>
313<!-- END CONTENT -->
314
315
316<div id="footer">
317<p>
318Previous Topic:&nbsp;&nbsp;<a href="call_function.html">Custom Function Calls</a>
319&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
320<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
321<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
admin3ed8c512006-09-29 23:26:28 +0000322Next Topic:&nbsp;&nbsp;<a href="caching.html">Database Caching Class</a>
admin3b60ae42006-09-25 23:26:03 +0000323<p>
324<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>
325</div>
326
327</body>
328</html>