Codeigniter Database Configuration


Codeigniter Database Configuration – Database refrence provides configuration that are used to deal with database operations. Codeigniter has a config file where we store our database connection values like (username, password, database name) etc. We can also set database connection values for specific environment by placing database.php in the respective environment config folder. Here in this tutorial, we are going to explain how to Database Configuration reference.

Codeigniter Database Configuration | Example.

Let us understand how to configure database in codeigniter with examples.

Database Configuration

The config settings are stored in a multi-dimensional array with this prototype.

Lets see how to configure database:

$db['default'] = array(
        'dsn'   => '',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'database_name',
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => TRUE,
        'db_debug' => TRUE,
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array()
);

Some database driver like(PDO, PostgreSQL, Oracle, ODBC) may require a full DSN string to be provided. In this case, you should use ‘dsn’ configuration setting. Like this:-

Lets see DSN configuration setting:

//PDO
$db['default']['dsn'] = 'pgsql:host=localhost;port=5432;dbname=database_name';

// Oracle
$db['default']['dsn'] = '//localhost/XE';

We can also use failovers for the situation when the main connection cannot connect for any reason. These failovers can be specified by setting the failovers for a connection like this:-

Syntax of failover connection:

$db['default']['failover'] = array(
                array(
                        'hostname' => 'localhost1',
                        'username' => '',
                        'password' => '',
                        'database' => '',
                        'dbdriver' => 'mysqli',
                        'dbprefix' => '',
                        'pconnect' => TRUE,
                        'db_debug' => TRUE,
                        'cache_on' => FALSE,
                        'cachedir' => '',
                        'char_set' => 'utf8',
                        'dbcollat' => 'utf8_general_ci',
                        'swap_pre' => '',
                        'encrypt' => FALSE,
                        'compress' => FALSE,
                        'stricton' => FALSE
                ),
                array(
                        'hostname' => 'localhost2',
                        'username' => '',
                        'password' => '',
                        'database' => '',
                        'dbdriver' => 'mysqli',
                        'dbprefix' => '',
                        'pconnect' => TRUE,
                        'db_debug' => TRUE,
                        'cache_on' => FALSE,
                        'cachedir' => '',
                        'char_set' => 'utf8',
                        'dbcollat' => 'utf8_general_ci',
                        'swap_pre' => '',
                        'encrypt' => FALSE,
                        'compress' => FALSE,
                        'stricton' => FALSE
                )
        );

Query Builder

The query builder class is globally enabled or disabled through setting the $query_builder. The default setting is TRUE.

Syntax of query builder:

$query_builder = TRUE;

Advertisements

Add Comment

📖 Read More