Code Igniter User Guide Version 1.5.0


Database Utilities Class

The Database Utilities Class contains functions that help you manage your database.

Important:  This class must be initialized independently since it is a separate class from the main Database class. More info below...

Initializing the Utilities Class

To initialize this class please use the following code:

$this->load->dbutil()

You can also autoload this class from within your config/autoload.php file by specifying dbutil in the $autoload['libraries'] array.

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

$this->dbutil->some_function()

$this->dbutil->create_database('db_name')

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

if ($this->dbutil->create_database('my_db'))
{
    echo 'Database created!';
}

$this->dbutil->drop_database('table_name')

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

if ($this->dbutil->drop_database('my_db'))
{
    echo 'Database deleted!';
}

$this->dbutil->list_databases()

Returns an array of database names:

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

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

$this->dbutil->list_tables();

Returns an array containing the names of all the tables in the database you are currently connected to. Example:

$tables = $this->dbutil->list_tables()

foreach ($tables as $table)
{
    echo $table;
}

$this->dbutil->table_exists();

Sometimes it's helpful to know whether a particular table exists before running an operation on it. Returns a boolean TRUE/FALSE. Usage example:

if ($this->dbutil->table_exists('table_name'))
{
    // some code...
}

Note: Replace table_name with the name of the table you are looking for.

$this->dbutil->optimize_table('table_name');

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.

$this->dbutil->repair_table('table_name');

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->optimize_table('table_name'))
{
    echo 'Success!'
}

Note: Not all database platforms support table repairs.

$this->dbutil->optimize_database();

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.

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

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

Note: Not all database platforms support table optimization.