blob: cbc79153784d65b4b6b0ae1f7061c1662048688e [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() {
admine334c472006-10-21 19:44:22 +000015 myHeight = new fx.Height('nav', {duration: 400});
admin3b60ae42006-09-25 23:26:03 +000016 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>
admine0cd6092006-10-20 01:00:31 +000036<td><h1>Code Igniter User Guide Version 1.5.0b3</h1></td>
admin4d02a4f2006-10-11 19:40:24 +000037<td id="breadcrumb_right"><a href="../toc.html">Linear Table of Contents</a></td>
admin3b60ae42006-09-25 23:26:03 +000038</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
admin572ed812006-10-12 18:01:52 +000089already be running, since the utilities class relies on it.</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
admin572ed812006-10-12 18:01:52 +000093<code>$this->load->dbutil()</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
adminf66de862006-10-10 23:30:04 +0000141<p class="important"><strong>Note:</strong>&nbsp; This features is only available for MySQL/MySQLi databases.</p>
142
143
admin3b60ae42006-09-25 23:26:03 +0000144<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>
145
146<code>
147if ($this->dbutil->optimize_table('table_name'))<br />
148{<br />
adminf66de862006-10-10 23:30:04 +0000149&nbsp;&nbsp;&nbsp; echo 'Success!';<br />
admin3b60ae42006-09-25 23:26:03 +0000150}
151</code>
152
153<p><strong>Note:</strong> Not all database platforms support table optimization.</p>
154
155
admin32cb0e52006-09-30 20:36:59 +0000156<a name="repair"></a>
admin3b60ae42006-09-25 23:26:03 +0000157<h2>$this->dbutil->repair_table('table_name');</h2>
158
adminf66de862006-10-10 23:30:04 +0000159<p class="important"><strong>Note:</strong>&nbsp; This features is only available for MySQL/MySQLi databases.</p>
160
161
admin3b60ae42006-09-25 23:26:03 +0000162<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>
163
164<code>
165if ($this->dbutil->optimize_table('table_name'))<br />
166{<br />
adminf66de862006-10-10 23:30:04 +0000167&nbsp;&nbsp;&nbsp; echo 'Success!';<br />
admin3b60ae42006-09-25 23:26:03 +0000168}
169</code>
170
171<p><strong>Note:</strong> Not all database platforms support table repairs.</p>
172
173
admin32cb0e52006-09-30 20:36:59 +0000174<a name="optdb"></a>
admine5bb9362006-09-27 00:31:22 +0000175<h2>$this->dbutil->optimize_database();</h2>
176
adminf66de862006-10-10 23:30:04 +0000177<p class="important"><strong>Note:</strong>&nbsp; This features is only available for MySQL/MySQLi databases.</p>
178
admine5bb9362006-09-27 00:31:22 +0000179<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>
180
181<code>
adminf66de862006-10-10 23:30:04 +0000182$result = $this->dbutil->optimize_database();<br />
admine5bb9362006-09-27 00:31:22 +0000183<br />
184if ($result !== FALSE)<br />
185{<br />
186&nbsp;&nbsp;&nbsp; print_r($result);<br />
187}
188</code>
189
190<p><strong>Note:</strong> Not all database platforms support table optimization.</p>
admin3b60ae42006-09-25 23:26:03 +0000191
192
admin24dd7e72006-10-01 19:02:29 +0000193<a name="csv"></a>
194<h2>$this->dbutil->csv_from_result($db_result)</h2>
admin3ed8c512006-09-29 23:26:28 +0000195
admine334c472006-10-21 19:44:22 +0000196<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 +0000197Example:</p>
198
199<code>
200$this->load->dbutil();<br />
201<br />
202$query = $this->db->query("SELECT * FROM mytable");<br />
203<br />
admin24dd7e72006-10-01 19:02:29 +0000204echo $this->dbutil->csv_from_result($query);
admin3ed8c512006-09-29 23:26:28 +0000205</code>
206
admine334c472006-10-21 19:44:22 +0000207<p>The second and third parameters allows you to
admin3ed8c512006-09-29 23:26:28 +0000208set the delimiter and newline character. By default tabs are used as the delimiter and "\n" is used as a new line. Example:
209
210<code>
211$delimiter = ",";<br />
212$newline = "\r\n";<br />
213<br />
admin24dd7e72006-10-01 19:02:29 +0000214echo $this->dbutil->csv_from_result($query, $delimiter, $newline);
admin3ed8c512006-09-29 23:26:28 +0000215</code>
216
adminf66de862006-10-10 23:30:04 +0000217<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 +0000218If you need to write the file use the <a href="../helpers/file_helper.html">File Helper</a>.</p>
219
220
admin32cb0e52006-09-30 20:36:59 +0000221<a name="xml"></a>
admin3ed8c512006-09-29 23:26:28 +0000222<h2>$this->dbutil->xml_from_result($db_result)</h2>
223
admine334c472006-10-21 19:44:22 +0000224<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 +0000225may contain an optional array of config parameters. Example:</p>
226
227<code>
228$this->load->dbutil();<br />
229<br />
230$query = $this->db->query("SELECT * FROM mytable");<br />
231<br />
232$config = array (<br />
233&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'root'&nbsp;&nbsp;&nbsp; => 'root',<br />
234&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'element' => 'element', <br />
235&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 +0000236&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 +0000237&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
238<br />
admin24dd7e72006-10-01 19:02:29 +0000239echo $this->dbutil->xml_from_result($query, $config);
admin3ed8c512006-09-29 23:26:28 +0000240</code>
241
adminf66de862006-10-10 23:30:04 +0000242<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 +0000243If you need to write the file use the <a href="../helpers/file_helper.html">File Helper</a>.</p>
244
245
admin32cb0e52006-09-30 20:36:59 +0000246<a name="backup"></a>
247<h2>$this->dbutil->backup()</h2>
admin3dd978f2006-09-30 19:24:45 +0000248
admine856ff42006-10-02 02:38:30 +0000249<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 +0000250
adminf66de862006-10-10 23:30:04 +0000251<p class="important"><strong>Note:</strong>&nbsp; This features is only available for MySQL/MySQLi databases.</p>
252
admin0e425542006-10-01 03:37:43 +0000253<p>Note: Due to the limited execution time and memory available to PHP, backing up very large
admine334c472006-10-21 19:44:22 +0000254databases 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 +0000255via the command line, or have your server admin do it for you if you do not have root privileges.</p>
256
admine856ff42006-10-02 02:38:30 +0000257<h3>Usage Example</h3>
admin3ed8c512006-09-29 23:26:28 +0000258
admine856ff42006-10-02 02:38:30 +0000259<code>
260<dfn>// Load the DB utility class</dfn><br />
261$this->load->dbutil();<br /><br />
262
adminc5f7fa32006-10-06 02:10:23 +0000263<dfn>// Backup your entire database and assign it to a variable</dfn><br />
admine856ff42006-10-02 02:38:30 +0000264$backup =& $this->dbutil->backup();
265
266<br /><br />
267<dfn>// Load the file helper and write the file to your server</dfn><br />
268$this->load->helper('file');<br />
269write_file('/path/to/mybackup.gz', $backup);
270
271<br /><br />
272<dfn>// Load the download helper and send the file to your desktop</dfn><br />
273$this->load->helper('download');<br />
274force_download('mybackup.gz', $backup);
admin32cb0e52006-09-30 20:36:59 +0000275</code>
admin3ed8c512006-09-29 23:26:28 +0000276
admin32cb0e52006-09-30 20:36:59 +0000277<h3>Setting Backup Preferences</h3>
278
admine856ff42006-10-02 02:38:30 +0000279<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 +0000280
281<code>$prefs = array(<br />
282&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 +0000283&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 />
284&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 />
285&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 />
286&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 />
287&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 />
288&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 +0000289&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
290<br />
291$this->dbutil->backup($prefs);
292</code>
293
admin0e425542006-10-01 03:37:43 +0000294
admin32cb0e52006-09-30 20:36:59 +0000295<h3>Description of Backup Preferences</h3>
296
297<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
298<tr>
299<th>Preference</th>
300<th>Default&nbsp;Value</th>
301<th>Options</th>
302<th>Description</th>
303</tr><tr>
admine856ff42006-10-02 02:38:30 +0000304<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 +0000305</tr><tr>
admine856ff42006-10-02 02:38:30 +0000306<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 +0000307</tr><tr>
308<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>
309</tr><tr>
admine856ff42006-10-02 02:38:30 +0000310<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 +0000311</tr><tr>
312<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>
313</tr><tr>
admin40037182006-10-11 19:16:58 +0000314<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 +0000315</tr><tr>
admin40037182006-10-11 19:16:58 +0000316<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 +0000317
318</tr>
319</table>
320
admin3b60ae42006-09-25 23:26:03 +0000321
322</div>
323<!-- END CONTENT -->
324
325
326<div id="footer">
327<p>
328Previous Topic:&nbsp;&nbsp;<a href="call_function.html">Custom Function Calls</a>
329&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
330<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
331<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
admin3ed8c512006-09-29 23:26:28 +0000332Next Topic:&nbsp;&nbsp;<a href="caching.html">Database Caching Class</a>
admin3b60ae42006-09-25 23:26:03 +0000333<p>
334<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>
335</div>
336
337</body>
338</html>