Database Utility Class

The Database Utility Class contains methods that help you manage your database.

Initializing the Utility Class

Important

In order to 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();

You can also pass another database object to the DB Utility loader, in case the database you want to manage isn’t the default one:

$this->myutil = $this->load->dbutil($this->other_db, TRUE);

In the above example, we’re passing a custom database object as the first parameter and then tell it to return the dbutil object, instead of assigning it directly to $this->dbutil.

Note

Both of the parameters can be used individually, just pass an empty value as the first one if you wish to skip it.

Once initialized you will access the methods using the $this->dbutil object:

$this->dbutil->some_method();

Using the Database Utilities

Retrieve list of database names

Returns an array of database names:

$dbs = $this->dbutil->list_databases();

foreach ($dbs as $db)
{
        echo $db;
}

Determine If a Database Exists

Sometimes it’s helpful to know whether a particular database exists. Returns a boolean TRUE/FALSE. Usage example:

if ($this->dbutil->database_exists('database_name'))
{
        // some code...
}

Note

Replace database_name with the name of the database you are looking for. This method is case sensitive.

Optimize a Table

Permits you to optimize a table using the table name specified in the first parameter. Returns TRUE/FALSE based on success or failure:

if ($this->dbutil->optimize_table('table_name'))
{
        echo 'Success!';
}

Note

Not all database platforms support table optimization. It is mostly for use with MySQL.

Repair a Table

Permits you to repair a table using the table name specified in the first parameter. Returns TRUE/FALSE based on success or failure:

if ($this->dbutil->repair_table('table_name'))
{
        echo 'Success!';
}

Note

Not all database platforms support table repairs.

Optimize a Database

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.

$result = $this->dbutil->optimize_database();

if ($result !== FALSE)
{
        print_r($result);
}

Note

Not all database platforms support database optimization. It it is mostly for use with MySQL.