Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ###################### |
| 2 | Database Configuration |
| 3 | ###################### |
| 4 | |
| 5 | CodeIgniter has a config file that lets you store your database |
| 6 | connection values (username, password, database name, etc.). The config |
| 7 | file is located at application/config/database.php. You can also set |
| 8 | database connection values for specific |
| 9 | :doc:`environments <../libraries/config>` by placing **database.php** |
| 10 | it the respective environment config folder. |
| 11 | |
| 12 | The config settings are stored in a multi-dimensional array with this |
| 13 | prototype:: |
| 14 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame^] | 15 | $db['default'] = array( |
| 16 | 'dsn' => '', |
| 17 | 'hostname' => 'localhost', |
| 18 | 'username' => 'root', |
| 19 | 'password' => '', |
| 20 | 'database' => 'database_name', |
| 21 | 'dbdriver' => 'mysqli', |
| 22 | 'dbprefix' => '', |
| 23 | 'pconnect' => TRUE, |
| 24 | 'db_debug' => TRUE, |
| 25 | 'cache_on' => FALSE, |
| 26 | 'cachedir' => '', |
| 27 | 'char_set' => 'utf8', |
| 28 | 'dbcollat' => 'utf8_general_ci', |
| 29 | 'swap_pre' => '', |
| 30 | 'autoinit' => TRUE, |
| 31 | 'stricton' => FALSE, |
| 32 | 'failover' => array() |
| 33 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 34 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame^] | 35 | Some database drivers (such as PDO, PostgreSQL, Oracle, ODBC) might |
| 36 | require a full DSN string to be provided. If that is the case, you |
| 37 | should use the 'dsn' configuration setting, as if you're using the |
| 38 | driver's underlying native PHP extension, like this:: |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 39 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame^] | 40 | // PDO |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 41 | $db['default']['dsn'] = 'pgsql:host=localhost;port=5432;dbname=database_name'; |
| 42 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame^] | 43 | // Oracle |
| 44 | $db['default']['dsn'] = '//localhost/XE'; |
| 45 | |
| 46 | .. note:: If you do not specify a DSN string for a driver that requires it, CodeIgniter |
| 47 | will try to build it with the rest of the provided settings. |
| 48 | |
| 49 | .. note:: If you provide a DSN string and it is missing some valid settings (e.g. the |
| 50 | database character set), which are present in the rest of the configuration |
| 51 | fields, CodeIgniter will append them. |
| 52 | |
Felix Balfoort | 85fe96d | 2011-11-29 16:27:53 +0100 | [diff] [blame] | 53 | You can also specify failovers for the situation when the main connection cannot connect for some reason. |
| 54 | These failovers can be specified by setting the failover for a connection like this:: |
| 55 | |
Felix Balfoort | 292a0f6 | 2011-11-29 22:29:33 +0100 | [diff] [blame] | 56 | $db['default']['failover'] = array( |
| 57 | array( |
| 58 | 'hostname' => 'localhost1', |
| 59 | 'username' => '', |
| 60 | 'password' => '', |
| 61 | 'database' => '', |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame^] | 62 | 'dbdriver' => 'mysqli', |
Felix Balfoort | 292a0f6 | 2011-11-29 22:29:33 +0100 | [diff] [blame] | 63 | 'dbprefix' => '', |
| 64 | 'pconnect' => TRUE, |
| 65 | 'db_debug' => TRUE, |
| 66 | 'cache_on' => FALSE, |
| 67 | 'cachedir' => '', |
| 68 | 'char_set' => 'utf8', |
| 69 | 'dbcollat' => 'utf8_general_ci', |
| 70 | 'swap_pre' => '', |
| 71 | 'autoinit' => TRUE, |
| 72 | 'stricton' => FALSE |
| 73 | ), |
| 74 | array( |
| 75 | 'hostname' => 'localhost2', |
| 76 | 'username' => '', |
| 77 | 'password' => '', |
| 78 | 'database' => '', |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame^] | 79 | 'dbdriver' => 'mysqli', |
Felix Balfoort | 292a0f6 | 2011-11-29 22:29:33 +0100 | [diff] [blame] | 80 | 'dbprefix' => '', |
| 81 | 'pconnect' => TRUE, |
| 82 | 'db_debug' => TRUE, |
| 83 | 'cache_on' => FALSE, |
| 84 | 'cachedir' => '', |
| 85 | 'char_set' => 'utf8', |
| 86 | 'dbcollat' => 'utf8_general_ci', |
| 87 | 'swap_pre' => '', |
| 88 | 'autoinit' => TRUE, |
| 89 | 'stricton' => FALSE |
| 90 | ) |
| 91 | ); |
Felix Balfoort | 85fe96d | 2011-11-29 16:27:53 +0100 | [diff] [blame] | 92 | |
Felix Balfoort | 68f5d49 | 2011-11-29 17:03:23 +0100 | [diff] [blame] | 93 | You can specify as many failovers as you like. |
Felix Balfoort | 85fe96d | 2011-11-29 16:27:53 +0100 | [diff] [blame] | 94 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 95 | The reason we use a multi-dimensional array rather than a more simple |
| 96 | one is to permit you to optionally store multiple sets of connection |
| 97 | values. If, for example, you run multiple environments (development, |
| 98 | production, test, etc.) under a single installation, you can set up a |
| 99 | connection group for each, then switch between groups as needed. For |
| 100 | example, to set up a "test" environment you would do this:: |
| 101 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame^] | 102 | $db['test'] = array( |
| 103 | 'dsn' => '', |
| 104 | 'hostname' => 'localhost', |
| 105 | 'username' => 'root', |
| 106 | 'password' => '', |
| 107 | 'database' => 'database_name', |
| 108 | 'dbdriver' => 'mysqli', |
| 109 | 'dbprefix' => '', |
| 110 | 'pconnect' => TRUE, |
| 111 | 'db_debug' => TRUE, |
| 112 | 'cache_on' => FALSE, |
| 113 | 'cachedir' => '', |
| 114 | 'char_set' => 'utf8', |
| 115 | 'dbcollat' => 'utf8_general_ci', |
| 116 | 'swap_pre' => '', |
| 117 | 'autoinit' => TRUE, |
| 118 | 'stricton' => FALSE, |
| 119 | 'failover' => array() |
| 120 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 121 | |
| 122 | Then, to globally tell the system to use that group you would set this |
| 123 | variable located in the config file:: |
| 124 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame^] | 125 | $active_group = 'test'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 126 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame^] | 127 | .. note:: The name 'test' is arbitrary. It can be anything you want. By |
| 128 | default we've used the word "default" for the primary connection, |
| 129 | but it too can be renamed to something more relevant to your project. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 130 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 131 | Query Builder |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 132 | ------------- |
| 133 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 134 | The :doc:`Query Builder Class <query_builder>` is globally enabled or |
| 135 | disabled by setting the $query_builder variable in the database |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 136 | configuration file to TRUE/FALSE (boolean). If you are not using the |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 137 | query builder class, setting it to FALSE will utilize fewer resources |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 138 | when the database classes are initialized. |
| 139 | |
| 140 | :: |
| 141 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 142 | $query_builder = TRUE; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 143 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame^] | 144 | .. note:: that some CodeIgniter classes such as Sessions require Query |
| 145 | Builder to be enabled to access certain functionality. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 146 | |
| 147 | Explanation of Values: |
| 148 | ---------------------- |
| 149 | |
purwandi | 3eed88c | 2011-10-07 09:47:21 +0700 | [diff] [blame] | 150 | ====================== ================================================================================================== |
| 151 | Name Config Description |
| 152 | ====================== ================================================================================================== |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame^] | 153 | **dsn** The DSN connect string (an all-in-one configuration sequence). |
| 154 | **hostname** The hostname of your database server. Often this is 'localhost'. |
purwandi | 3eed88c | 2011-10-07 09:47:21 +0700 | [diff] [blame] | 155 | **username** The username used to connect to the database. |
| 156 | **password** The password used to connect to the database. |
| 157 | **database** The name of the database you want to connect to. |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame^] | 158 | **dbdriver** The database type. ie: mysqli, postgre, odbc, etc. Must be specified in lower case. |
purwandi | 3eed88c | 2011-10-07 09:47:21 +0700 | [diff] [blame] | 159 | **dbprefix** An optional table prefix which will added to the table name when running :doc: |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 160 | `Query Builder <query_builder>` queries. This permits multiple CodeIgniter installations |
purwandi | 3eed88c | 2011-10-07 09:47:21 +0700 | [diff] [blame] | 161 | to share one database. |
| 162 | **pconnect** TRUE/FALSE (boolean) - Whether to use a persistent connection. |
| 163 | **db_debug** TRUE/FALSE (boolean) - Whether database errors should be displayed. |
| 164 | **cache_on** TRUE/FALSE (boolean) - Whether database query caching is enabled, |
| 165 | see also :doc:`Database Caching Class <caching>`. |
| 166 | **cachedir** The absolute server path to your database query cache directory. |
| 167 | **char_set** The character set used in communicating with the database. |
| 168 | **dbcollat** The character collation used in communicating with the database |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 169 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame^] | 170 | .. note:: Only used in the 'mysql' and 'mysqli' drivers. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 171 | |
purwandi | 3eed88c | 2011-10-07 09:47:21 +0700 | [diff] [blame] | 172 | **swap_pre** A default table prefix that should be swapped with dbprefix. This is useful for distributed |
| 173 | applications where you might run manually written queries, and need the prefix to still be |
| 174 | customizable by the end user. |
| 175 | **autoinit** Whether or not to automatically connect to the database when the library loads. If set to false, |
| 176 | the connection will take place prior to executing the first query. |
| 177 | **stricton** TRUE/FALSE (boolean) - Whether to force "Strict Mode" connections, good for ensuring strict SQL |
| 178 | while developing an application. |
purwandi | abb456a | 2011-10-07 09:52:46 +0700 | [diff] [blame] | 179 | **port** The database port number. To use this value you have to add a line to the database config array. |
purwandi | 3eed88c | 2011-10-07 09:47:21 +0700 | [diff] [blame] | 180 | :: |
Greg Aker | ffd24a4 | 2011-12-25 22:27:59 -0600 | [diff] [blame] | 181 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame^] | 182 | $db['default']['port'] = 5432; |
purwandi | 3eed88c | 2011-10-07 09:47:21 +0700 | [diff] [blame] | 183 | ====================== ================================================================================================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 184 | |
Andrey Andreev | 2f3beb2 | 2012-03-12 16:34:15 +0200 | [diff] [blame] | 185 | .. note:: Depending on what database platform you are using (MySQL, PostgreSQL, |
| 186 | etc.) not all values will be needed. For example, when using SQLite you |
| 187 | will not need to supply a username or password, and the database name |
| 188 | will be the path to your database file. The information above assumes |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame^] | 189 | you are using MySQL. |