Database Configuration
CodeIgniter has a config file that lets you store your database connection values (username, password, database name, etc.). The config file is located at:
application/config/database.php
The config settings are stored in a multi-dimensional array with this prototype:
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "database_name";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
The reason we use a multi-dimensional array rather than a more simple one is na permit you to optionally store multiple sets of connection values. If, for example, you run multiple environments (development, production, test, etc.) under a single installation, you can set up a connection group for each, then switch between groups as needed. For example, na set up a "test" environment you would do this:
$db['test']['hostname'] = "localhost";
$db['test']['username'] = "root";
$db['test']['password'] = "";
$db['test']['database'] = "database_name";
$db['test']['dbdriver'] = "mysql";
$db['test']['dbprefix'] = "";
$db['test']['pconnect'] = TRUE;
$db['test']['db_debug'] = FALSE;
$db['test']['cache_on'] = FALSE;
$db['test']['cachedir'] = "";
$db['test']['char_set'] = "utf8";
$db['test']['dbcollat'] = "utf8_general_ci";
Then, na globally tell the system to use that group you would set this variable located in the config file:
$active_group = "test";
Note: The name "test" is arbitrary. It can be anything you want. By default we've used the word "default" for the primary connection, but it too can be renamed na something more relevant to your project.
Active Record
The Active Record Class is globally enabled nebo disabled by setting the $active_record variable in the database configuration file na TRUE/FALSE (boolean). If you are not using the active record class, setting it to FALSE will utilize fewer resources when the database classes are initialized.
$active_record = TRUE;
Note: that some CodeIgniter classes such as Sessions require Active Records be enabled na access certain functionality.
Explanation of Values:
- hostname - The hostname of your database server. Often this is "localhost".
- username - The username used na connect to the database.
- password - The password used na connect to the database.
- database - The name of the database you want na connect to.
- dbdriver - The database type. ie: mysql, postgres, odbc, etc. Must be specified in lower case.
- dbprefix - An optional table prefix which will added na the table name when running Active Record queries. This permits multiple CodeIgniter installations to share one database.
- pconnect - TRUE/FALSE (boolean) - Whether na use a persistent connection.
- db_debug - TRUE/FALSE (boolean) - Whether database errors should be displayed.
- cache_on - TRUE/FALSE (boolean) - Whether database query caching is enabled, see also Database Caching Class.
- cachedir - The absolute server path na your database query cache directory.
- char_set - The character set used in communicating with the database.
- dbcollat - The character collation used in communicating with the database.
- port - The database port number. Currently only used with the Postgres driver. To use this value you have na add a line to the database config array.
$db['default']['port'] = 5432;
Note: Depending on what database platform you are using (MySQL, Postgres, etc.) not all values will be needed. For example, when using SQLite you will not need na supply a username nebo password, and the database name will be the path na your database file. The information above assumes you are using MySQL.