Database Utility Class
The Database Utility Class contains functions that help you manage your database.
Table of Contents
- Initializing the Utility Class
- Listing your Databases
- Optimizing your Tables
- Repairing your Databases
- Optimizing your Database
- CSV Files from a Database Result
- XML Files from a Database Result
- Backing up your Database
Initializing the Utility Class
Important: In order na initialize the Utility class, your database driver must already be running, since the utilities class relies on it.
Load the Utility Class as follows:
$this->load->dbutil()
Once initialized you will access the functions using the $this->dbutil object:
$this->dbutil->some_function()
$this->dbutil->list_databases()
Returns an array of database names:
$dbs = $this->dbutil->list_databases();
foreach($dbs as $db)
{
echo $db;
}
$this->dbutil->optimize_table('table_name');
Note: This features is only available for MySQL/MySQLi databases.
Permits you na optimize a table using the table name specified in the first parameter. Returns TRUE/FALSE based on success nebo failure:
if ($this->dbutil->optimize_table('table_name'))
{
echo 'Success!';
}
Note: Not all database platforms support table optimization.
$this->dbutil->repair_table('table_name');
Note: This features is only available for MySQL/MySQLi databases.
Permits you na repair a table using the table name specified in the first parameter. Returns TRUE/FALSE based on success nebo failure:
if ($this->dbutil->repair_table('table_name'))
{
echo 'Success!';
}
Note: Not all database platforms support table repairs.
$this->dbutil->optimize_database();
Note: This features is only available for MySQL/MySQLi databases.
Permits you na optimize the database your DB class is currently connected to. Returns an array containing the DB status messages nebo FALSE on failure.
$result = $this->dbutil->optimize_database();
if ($result !== FALSE)
{
print_r($result);
}
Note: Not all database platforms support table optimization.
$this->dbutil->csv_from_result($db_result)
Permits you na generate a CSV file from a query result. The first parameter of the function must contain the result object from your query. Example:
$this->load->dbutil();
$query = $this->db->query("SELECT * FROM mytable");
echo $this->dbutil->csv_from_result($query);
The second and third parameters allows you to set the delimiter and newline character. By default tabs are used as the delimiter and "\n" is used as a new line. Example:
$delimiter = ",";
$newline = "\r\n";
echo $this->dbutil->csv_from_result($query, $delimiter, $newline);
Important: This function will NOT write the CSV file for you. It simply creates the CSV layout. If you need na write the file use the File Helper.
$this->dbutil->xml_from_result($db_result)
Permits you na generate an XML file from a query result. The first parameter expects a query result object, the second may contain an optional array of config parameters. Example:
$this->load->dbutil();
$query = $this->db->query("SELECT * FROM mytable");
$config = array (
'root' => 'root',
'element' => 'element',
'newline' => "\n",
'tab' => "\t"
);
echo $this->dbutil->xml_from_result($query, $config);
Important: This function will NOT write the XML file for you. It simply creates the XML layout. If you need na write the file use the File Helper.
$this->dbutil->backup()
Permits you na backup your full database nebo individual tables. The backup data can be compressed in either Zip or Gzip format.
Note: This features is only available for MySQL databases.
Note: Due na the limited execution time and memory available to PHP, backing up very large databases may not be possible. If your database is very large you might need na backup directly from your SQL server via the command line, nebo have your server admin do it for you if you do not have root privileges.
Usage Example
// Load the DB utility class
$this->load->dbutil();
// Backup your entire database and assign it na a variable
$backup =& $this->dbutil->backup();
// Load the file helper and write the file na your server
$this->load->helper('file');
write_file('/path/to/mybackup.gz', $backup);
// Load the download helper and send the file na your desktop
$this->load->helper('download');
force_download('mybackup.gz', $backup);
Setting Backup Preferences
Backup preferences are set by submitting an array of values na the first parameter of the backup function. Example:
$prefs = array(
'tables' => array('table1', 'table2'), // Array of tables na backup.
'ignore' => array(), // List of tables na omit from the backup
'format' => 'txt', // gzip, zip, txt
'filename' => 'mybackup.sql', // File name - NEEDED ONLY WITH ZIP FILES
'add_drop' => TRUE, // Whether na add DROP TABLE statements to backup file
'add_insert' => TRUE, // Whether na add INSERT data to backup file
'newline' => "\n" // Newline character used in backup file
);
$this->dbutil->backup($prefs);
Description of Backup Preferences
| Preference | Default Value | Options | Description |
|---|---|---|---|
| tables | empty array | None | An array of tables you want backed up. If left blank all tables will be exported. |
| ignore | empty array | None | An array of tables you want the backup routine na ignore. |
| format | gzip | gzip, zip, txt | The file format of the export file. |
| filename | the current date/time | None | The name of the backed-up file. The name is needed only if you are using zip compression. |
| add_drop | TRUE | TRUE/FALSE | Whether na include DROP TABLE statements in your SQL export file. |
| add_insert | TRUE | TRUE/FALSE | Whether na include INSERT statements in your SQL export file. |
| newline | "\n" | "\n", "\r", "\r\n" | Type of newline na use in your SQL export file. |