CodeIgniter Database Configuration

Database Support

The database.php file contains all the information to connect to a database. A Database is required for most web application programming. CodeIgniter supported databases are MySQL (4.1+), MySQLi, MS SQL, Postgres, Oracle, SQLite, and ODBC

Active Group

In codeigniter database configurtion, we could create number of db connection. The $active_group variable lets you choose which connection group to make active. By default there is only one group (the ‘default’ group).

$active_group = 'default';

Database Credentials

  1. To Connect to database, We have to store the following valid information into ‘$db’ array. They are hostname, username, passwor, database name and database driver details.
  2. ‘$db’ array values are stored under the ‘default’ group. We could have create number of groups with different configuration.
  3. See the “database.php” file, You will see ‘$db[‘default’]’ database configuration values.

database.php File

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'DATABASE_HOST';
$db['default']['username'] = 'DATABASE_USERNAME';
$db['default']['password'] = 'DATABASE_PASSWORD';
$db['default']['database'] = 'DATABASE_NAME';
$db['default']['dbdriver'] = 'DATABASE_DRIVER'; // mysql, Postgres, oracle, sqlite, etc
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

Explanation Of Variables

  • [‘hostname’] – The hostname of your database server.
  • [‘username’] – The username used to connect to the database
  • [‘password’] – The password used to connect to the database
  • [‘database’] – The name of the database you want to connect to
  • [‘dbdriver’] – The database type. ie: mysql. Currently supported: mysql, mysqli, postgre, odbc, mssql, sqlite, oci8
  • [‘dbprefix’] – You can add an optional prefix, which will be added to the table name when using the Active Record class
  • [‘pconnect’] – TRUE/FALSE – Whether to use a persistent connection
  • [‘db_debug’] – TRUE/FALSE – Whether database errors should be displayed.
  • [‘cache_on’] – TRUE/FALSE – Enables/disables query caching
  • [‘cachedir’] – The path to the folder where cache files should be stored
  • [‘char_set’] – The character set used in communicating with the database
  • [‘dbcollat’] – The character collation used in communicating with the database NOTE: For MySQL and MySQLi databases, this setting is only used as a backup if your server is running PHP < 5.2.3 or MySQL < 5.0.7 (and in table creation queries made with DB Forge). There is an incompatibility in PHP with mysql_real_escape_string() which can make your site vulnerable to SQL injection if you are using a multi-byte character set and are running versions lower than these. Sites using Latin-1 or UTF-8 database character set and collation are unaffected.
  • [‘swap_pre’] – A default table prefix that should be swapped with the dbprefix
  • [‘autoinit’] – Whether or not to automatically initialize the database.
  • [‘stricton’] – TRUE/FALSE – forces ‘Strict Mode’ connections good for ensuring strict SQL while developing

Leave a Reply

Your email address will not be published. Required fields are marked *