blob: 2cc3f52198af01b58a906e9659bac6303ca3a567 [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;
51Database Utilities Class
52</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
65<h1>Database Utilities Class</h1>
66
67<p>The Database Utilities Class contains functions that help you manage your database.</p>
68
69<p class="important"><strong>Important:</strong>&nbsp; This class must be initialized independently since it is a separate class from the main Database class.
70More info below...</p>
71
72
73<h2>Initializing the Utilities Class</h2>
74
75<p>To initialize this class please use the following code:</p>
76
77<code>$this->load->dbutil()</code>
78
79<p>You can also autoload this class from within your <dfn>config/autoload.php</dfn> file by specifying <kbd>dbutil</kbd> in the <samp>$autoload['libraries']</samp> array.</p>
80
81<p>Once initialized you will access the functions using the <dfn>$this->dbutil</dfn> object:</p>
82
83<code>$this->dbutil->some_function()</code>
84
85
86
87<h2>$this->dbutil->create_database('db_name')</h2>
88
89<p>Permits you to create a database using the name specified in the first parameter. Returns TRUE/FALSE based on success or failure:</p>
90
91<code>if ($this->dbutil->create_database('my_db'))<br />
92{<br />
93&nbsp;&nbsp;&nbsp; echo 'Database created!';<br />
94}</code>
95
96
97<h2>$this->dbutil->drop_database('table_name')</h2>
98
99<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>
100
101<code>if ($this->dbutil->drop_database('my_db'))<br />
102{<br />
103&nbsp;&nbsp;&nbsp; echo 'Database deleted!';<br />
104}</code>
105
106
107<h2>$this->dbutil->list_databases()</h2>
108
109<p>Returns an array of database names:</p>
110
111
112<code>
113$dbs = $this->dbutil->list_databases();<br />
114<br />
115foreach($dbs as $db)<br />
116{<br />
117&nbsp;&nbsp;&nbsp; echo $db;<br />
118}</code>
119
120
121
admin3b60ae42006-09-25 23:26:03 +0000122<h2>$this->dbutil->optimize_table('table_name');</h2>
123
124<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>
125
126<code>
127if ($this->dbutil->optimize_table('table_name'))<br />
128{<br />
129&nbsp;&nbsp;&nbsp; echo 'Success!'<br />
130}
131</code>
132
133<p><strong>Note:</strong> Not all database platforms support table optimization.</p>
134
135
136<h2>$this->dbutil->repair_table('table_name');</h2>
137
138<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>
139
140<code>
141if ($this->dbutil->optimize_table('table_name'))<br />
142{<br />
143&nbsp;&nbsp;&nbsp; echo 'Success!'<br />
144}
145</code>
146
147<p><strong>Note:</strong> Not all database platforms support table repairs.</p>
148
149
150
admine5bb9362006-09-27 00:31:22 +0000151<h2>$this->dbutil->optimize_database();</h2>
152
153<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>
154
155<code>
156$result = $this->dbutil->optimize_databas();<br />
157<br />
158if ($result !== FALSE)<br />
159{<br />
160&nbsp;&nbsp;&nbsp; print_r($result);<br />
161}
162</code>
163
164<p><strong>Note:</strong> Not all database platforms support table optimization.</p>
admin3b60ae42006-09-25 23:26:03 +0000165
166
167
admin3ed8c512006-09-29 23:26:28 +0000168<h2>$this->dbutil->cvs_from_result($db_result)</h2>
169
170<p>Permits you to generate a CVS file from a query result. The first parameter of the function must contain the result object from your query.
171Example:</p>
172
173<code>
174$this->load->dbutil();<br />
175<br />
176$query = $this->db->query("SELECT * FROM mytable");<br />
177<br />
178echo $this->dbutil->cvs_from_result($query);
179</code>
180
181<p>The second and third parameters allows you to
182set the delimiter and newline character. By default tabs are used as the delimiter and "\n" is used as a new line. Example:
183
184<code>
185$delimiter = ",";<br />
186$newline = "\r\n";<br />
187<br />
188echo $this->dbutil->cvs_from_result($query, $delimiter, $newline);
189</code>
190
191<p class="important"><strong>Important:</strong>&nbsp; This function will NOT write the CVS file for you. It simply creates the CVS layout.
192If you need to write the file use the <a href="../helpers/file_helper.html">File Helper</a>.</p>
193
194
195
196<h2>$this->dbutil->xml_from_result($db_result)</h2>
197
198<p>Permits you to generate an XML file from a query result. The first parameter expects a query result object, the second
199may contain an optional array of config parameters. Example:</p>
200
201<code>
202$this->load->dbutil();<br />
203<br />
204$query = $this->db->query("SELECT * FROM mytable");<br />
205<br />
206$config = array (<br />
207&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'root'&nbsp;&nbsp;&nbsp; => 'root',<br />
208&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'element' => 'element', <br />
209&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'newline' => "\n", <br />
210&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 />
211&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
212<br />
213echo $this->dbutil->cvs_from_result($query, $config);
214</code>
215
216<p class="important"><strong>Important:</strong>&nbsp; This function will NOT write the CVS file for you. It simply creates the CVS layout.
217If you need to write the file use the <a href="../helpers/file_helper.html">File Helper</a>.</p>
218
219
admin3dd978f2006-09-30 19:24:45 +0000220<h2>$this->dbutil->export()</h2>
221
222<p>Permits you to export your database or any desired tables. Export data can be downloaded to your desktop, archived to your
223server, or simply displayed. Archived files can be compressed.
admin3ed8c512006-09-29 23:26:28 +0000224
225
226
admin3b60ae42006-09-25 23:26:03 +0000227
228</div>
229<!-- END CONTENT -->
230
231
232<div id="footer">
233<p>
234Previous Topic:&nbsp;&nbsp;<a href="call_function.html">Custom Function Calls</a>
235&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
236<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
237<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
admin3ed8c512006-09-29 23:26:28 +0000238Next Topic:&nbsp;&nbsp;<a href="caching.html">Database Caching Class</a>
admin3b60ae42006-09-25 23:26:03 +0000239<p>
240<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>
241</div>
242
243</body>
244</html>