Subversion-Projekte lars-tiefland.codeigniter

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<!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>
31
<td><h1>CodeIgniter User Guide Version 1.7.1</h1></td>
32
<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;
46
Database 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>
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>
72
<li><a href="#csv">CSV Files from a Database Result</a></li>
73
<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
 
78
 
79
<h2><a name="init"></a>Initializing the Utility Class</h2>
80
 
81
<p class="important"><strong>Important:</strong>&nbsp; In order to initialize the Utility class, your database driver must
82
already be running, since the utilities class relies on it.</p>
83
 
84
<p>Load the Utility Class as follows:</p>
85
 
86
<code>$this->load->dbutil()</code>
87
 
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
 
92
<h2><a name="list"></a>$this->dbutil->list_databases()</h2>
93
<p>Returns an array of database names:</p>
94
 
95
<code>
96
$dbs = $this->dbutil->list_databases();<br />
97
<br />
98
foreach($dbs as $db)<br />
99
{<br />
100
&nbsp;&nbsp;&nbsp; echo $db;<br />
101
}</code>
102
<h2><a name="opttb"></a>$this->dbutil->optimize_table('table_name');</h2>
103
 
104
<p class="important"><strong>Note:</strong>&nbsp; This features is only available for MySQL/MySQLi databases.</p>
105
 
106
 
107
<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>
110
if ($this->dbutil->optimize_table('table_name'))<br />
111
{<br />
112
&nbsp;&nbsp;&nbsp; echo 'Success!';<br />
113
}
114
</code>
115
 
116
<p><strong>Note:</strong> Not all database platforms support table optimization.</p>
117
 
118
 
119
<h2><a name="repair"></a>$this->dbutil->repair_table('table_name');</h2>
120
 
121
<p class="important"><strong>Note:</strong>&nbsp; This features is only available for MySQL/MySQLi databases.</p>
122
 
123
 
124
<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>
127
if ($this->dbutil->repair_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 repairs.</p>
134
 
135
 
136
<h2><a name="optdb"></a>$this->dbutil->optimize_database();</h2>
137
 
138
<p class="important"><strong>Note:</strong>&nbsp; This features is only available for MySQL/MySQLi databases.</p>
139
 
140
<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>
141
 
142
<code>
143
$result = $this->dbutil->optimize_database();<br />
144
<br />
145
if ($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>
152
 
153
 
154
<h2><a name="csv"></a>$this->dbutil->csv_from_result($db_result)</h2>
155
 
156
<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.
157
Example:</p>
158
 
159
<code>
160
$this->load->dbutil();<br />
161
<br />
162
$query = $this->db->query("SELECT * FROM mytable");<br />
163
<br />
164
echo $this->dbutil->csv_from_result($query);
165
</code>
166
 
167
<p>The second and third parameters allows you to
168
set the delimiter and newline character.  By default tabs are used as the delimiter and "\n" is used as a new line.  Example:</p>
169
 
170
<code>
171
$delimiter = ",";<br />
172
$newline = "\r\n";<br />
173
<br />
174
echo $this->dbutil->csv_from_result($query, $delimiter, $newline);
175
</code>
176
 
177
<p><strong>Important:</strong>&nbsp; This function will NOT write the CSV file for you.  It simply creates the CSV layout.
178
If you need to write the file use the <a href="../helpers/file_helper.html">File Helper</a>.</p>
179
 
180
 
181
<h2><a name="xml"></a>$this->dbutil->xml_from_result($db_result)</h2>
182
 
183
<p>Permits you to generate an XML file from a query result. The first parameter expects a query result object, the second
184
may 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 />
195
&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 />
196
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
197
<br />
198
echo $this->dbutil->xml_from_result($query, $config);
199
</code>
200
 
201
<p><strong>Important:</strong>&nbsp; This function will NOT write the XML file for you.  It simply creates the XML layout.
202
If you need to write the file use the <a href="../helpers/file_helper.html">File Helper</a>.</p>
203
 
204
 
205
<h2><a name="backup"></a>$this->dbutil->backup()</h2>
206
 
207
<p>Permits you to backup your full database or individual tables.  The backup data can be compressed in either Zip or Gzip format.</p>
208
 
209
<p class="important"><strong>Note:</strong>&nbsp; This features is only available for MySQL databases.</p>
210
 
211
<p>Note: Due to the limited execution time and memory available to PHP, backing up very large
212
databases may not be possible.  If your database is very large you might need to backup directly from your SQL server
213
via the command line, or have your server admin do it for you if you do not have root privileges.</p>
214
 
215
<h3>Usage Example</h3>
216
 
217
<code>
218
<dfn>// Load the DB utility class</dfn><br />
219
$this->load->dbutil();<br /><br />
220
 
221
<dfn>// Backup your entire database and assign it to a variable</dfn><br />
222
$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 />
227
write_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 />
232
force_download('mybackup.gz', $backup);
233
</code>
234
 
235
<h3>Setting Backup Preferences</h3>
236
 
237
<p>Backup preferences are set by submitting an array of values to the first parameter of the backup function. Example:</p>
238
 
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 />
241
&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 />
247
&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
 
252
 
253
<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>
262
<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>
263
</tr><tr>
264
<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>
265
</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>
268
<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>
269
</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>
272
<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>
273
</tr><tr>
274
<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>
275
 
276
</tr>
277
</table>
278
 
279
 
280
</div>
281
<!-- END CONTENT -->
282
 
283
 
284
<div id="footer">
285
<p>
286
Previous Topic:&nbsp;&nbsp;<a href="forge.html">DB Forge Class</a>
287
&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;
290
Next Topic:&nbsp;&nbsp;<a href="../libraries/email.html"> Email Class</a></p>
291
<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2008 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
292
</div>
293
 
294
</body>
295
</html>