blob: 60a2026e5c56b50546d60a50855f9689da60ba00 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001######################
2Database Configuration
3######################
4
5CodeIgniter has a config file that lets you store your database
6connection values (username, password, database name, etc.). The config
7file is located at application/config/database.php. You can also set
8database connection values for specific
9:doc:`environments <../libraries/config>` by placing **database.php**
10it the respective environment config folder.
11
12The config settings are stored in a multi-dimensional array with this
13prototype::
14
purwandi3eed88c2011-10-07 09:47:21 +070015 $db['default']['hostname'] = "localhost";
16 $db['default']['username'] = "root";
17 $db['default']['password'] = "";
18 $db['default']['database'] = "database_name";
19 $db['default']['dbdriver'] = "mysql";
20 $db['default']['dbprefix'] = "";
21 $db['default']['pconnect'] = TRUE;
22 $db['default']['db_debug'] = FALSE;
23 $db['default']['cache_on'] = FALSE;
24 $db['default']['cachedir'] = "";
25 $db['default']['char_set'] = "utf8";
26 $db['default']['dbcollat'] = "utf8_general_ci";
27 $db['default']['swap_pre'] = "";
28 $db['default']['autoinit'] = TRUE;
29 $db['default']['stricton'] = FALSE;
Derek Jones8ede1a22011-10-05 13:34:52 -050030
Felix Balfoort85fe96d2011-11-29 16:27:53 +010031You can also specify failovers for the situation when the main connection cannot connect for some reason.
32These failovers can be specified by setting the failover for a connection like this::
33
34 $db['default']['failover'][0]['hostname'] = 'localhost1';
35 $db['default']['failover'][0]['username'] = '';
36 $db['default']['failover'][0]['password'] = '';
37 $db['default']['failover'][0]['database'] = '';
38 $db['default']['failover'][0]['dbdriver'] = 'mysql';
39 $db['default']['failover'][0]['dbprefix'] = '';
40 $db['default']['failover'][0]['pconnect'] = TRUE;
41 $db['default']['failover'][0]['db_debug'] = TRUE;
42 $db['default']['failover'][0]['cache_on'] = FALSE;
43 $db['default']['failover'][0]['cachedir'] = '';
44 $db['default']['failover'][0]['char_set'] = 'utf8';
45 $db['default']['failover'][0]['dbcollat'] = 'utf8_general_ci';
46 $db['default']['failover'][0]['swap_pre'] = '';
47 $db['default']['failover'][0]['autoinit'] = TRUE;
48 $db['default']['failover'][0]['stricton'] = FALSE;
49 $db['default']['failover'][0]['failover'] = array();
50
Felix Balfoort68f5d492011-11-29 17:03:23 +010051You can specify as many failovers as you like.
Felix Balfoort85fe96d2011-11-29 16:27:53 +010052
Derek Jones8ede1a22011-10-05 13:34:52 -050053The reason we use a multi-dimensional array rather than a more simple
54one is to permit you to optionally store multiple sets of connection
55values. If, for example, you run multiple environments (development,
56production, test, etc.) under a single installation, you can set up a
57connection group for each, then switch between groups as needed. For
58example, to set up a "test" environment you would do this::
59
purwandi3eed88c2011-10-07 09:47:21 +070060 $db['test']['hostname'] = "localhost";
61 $db['test']['username'] = "root";
62 $db['test']['password'] = "";
63 $db['test']['database'] = "database_name";
64 $db['test']['dbdriver'] = "mysql";
65 $db['test']['dbprefix'] = "";
66 $db['test']['pconnect'] = TRUE;
67 $db['test']['db_debug'] = FALSE;
68 $db['test']['cache_on'] = FALSE;
69 $db['test']['cachedir'] = "";
70 $db['test']['char_set'] = "utf8";
71 $db['test']['dbcollat'] = "utf8_general_ci";
72 $db['test']['swap_pre'] = "";
73 $db['test']['autoinit'] = TRUE;
74 $db['test']['stricton'] = FALSE;
Derek Jones8ede1a22011-10-05 13:34:52 -050075
76Then, to globally tell the system to use that group you would set this
77variable located in the config file::
78
79 $active_group = "test";
80
81Note: The name "test" is arbitrary. It can be anything you want. By
82default we've used the word "default" for the primary connection, but it
83too can be renamed to something more relevant to your project.
84
85Active Record
86-------------
87
88The :doc:`Active Record Class <active_record>` is globally enabled or
89disabled by setting the $active_record variable in the database
90configuration file to TRUE/FALSE (boolean). If you are not using the
91active record class, setting it to FALSE will utilize fewer resources
92when the database classes are initialized.
93
94::
95
96 $active_record = TRUE;
97
98.. note:: that some CodeIgniter classes such as Sessions require Active
99 Records be enabled to access certain functionality.
100
101Explanation of Values:
102----------------------
103
purwandi3eed88c2011-10-07 09:47:21 +0700104====================== ==================================================================================================
105 Name Config Description
106====================== ==================================================================================================
107**hostname** The hostname of your database server. Often this is "localhost".
108**username** The username used to connect to the database.
109**password** The password used to connect to the database.
110**database** The name of the database you want to connect to.
111**dbdriver** The database type. ie: mysql, postgres, odbc, etc. Must be specified in lower case.
112**dbprefix** An optional table prefix which will added to the table name when running :doc:
113 `Active Record <active_record>` queries. This permits multiple CodeIgniter installations
114 to share one database.
115**pconnect** TRUE/FALSE (boolean) - Whether to use a persistent connection.
116**db_debug** TRUE/FALSE (boolean) - Whether database errors should be displayed.
117**cache_on** TRUE/FALSE (boolean) - Whether database query caching is enabled,
118 see also :doc:`Database Caching Class <caching>`.
119**cachedir** The absolute server path to your database query cache directory.
120**char_set** The character set used in communicating with the database.
121**dbcollat** The character collation used in communicating with the database
Derek Jones8ede1a22011-10-05 13:34:52 -0500122
purwandi3eed88c2011-10-07 09:47:21 +0700123 .. note:: For MySQL and MySQLi databases, this setting is only used
124 as a backup if your server is running PHP < 5.2.3 or MySQL < 5.0.7
125 (and in table creation queries made with DB Forge). There is an
126 incompatibility in PHP with mysql_real_escape_string() which can
127 make your site vulnerable to SQL injection if you are using a
128 multi-byte character set and are running versions lower than these.
129 Sites using Latin-1 or UTF-8 database character set and collation are
130 unaffected.
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
purwandi3eed88c2011-10-07 09:47:21 +0700132**swap_pre** A default table prefix that should be swapped with dbprefix. This is useful for distributed
133 applications where you might run manually written queries, and need the prefix to still be
134 customizable by the end user.
135**autoinit** Whether or not to automatically connect to the database when the library loads. If set to false,
136 the connection will take place prior to executing the first query.
137**stricton** TRUE/FALSE (boolean) - Whether to force "Strict Mode" connections, good for ensuring strict SQL
138 while developing an application.
purwandiabb456a2011-10-07 09:52:46 +0700139**port** The database port number. To use this value you have to add a line to the database config array.
purwandi3eed88c2011-10-07 09:47:21 +0700140 ::
141 $db['default']['port'] = 5432;
142====================== ==================================================================================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500143
144.. note:: Depending on what database platform you are using (MySQL,
145 Postgres, etc.) not all values will be needed. For example, when using
146 SQLite you will not need to supply a username or password, and the
147 database name will be the path to your database file. The information
148 above assumes you are using MySQL.