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 | |
| 15 | $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"; $db['default']['swap_pre'] = ""; $db['default']['autoinit'] = TRUE; $db['default']['stricton'] = FALSE; |
| 16 | |
| 17 | The reason we use a multi-dimensional array rather than a more simple |
| 18 | one is to permit you to optionally store multiple sets of connection |
| 19 | values. If, for example, you run multiple environments (development, |
| 20 | production, test, etc.) under a single installation, you can set up a |
| 21 | connection group for each, then switch between groups as needed. For |
| 22 | example, to set up a "test" environment you would do this:: |
| 23 | |
| 24 | $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"; $db['test']['swap_pre'] = ""; $db['test']['autoinit'] = TRUE; $db['test']['stricton'] = FALSE; |
| 25 | |
| 26 | Then, to globally tell the system to use that group you would set this |
| 27 | variable located in the config file:: |
| 28 | |
| 29 | $active_group = "test"; |
| 30 | |
| 31 | Note: The name "test" is arbitrary. It can be anything you want. By |
| 32 | default we've used the word "default" for the primary connection, but it |
| 33 | too can be renamed to something more relevant to your project. |
| 34 | |
| 35 | Active Record |
| 36 | ------------- |
| 37 | |
| 38 | The :doc:`Active Record Class <active_record>` is globally enabled or |
| 39 | disabled by setting the $active_record variable in the database |
| 40 | configuration file to TRUE/FALSE (boolean). If you are not using the |
| 41 | active record class, setting it to FALSE will utilize fewer resources |
| 42 | when the database classes are initialized. |
| 43 | |
| 44 | :: |
| 45 | |
| 46 | $active_record = TRUE; |
| 47 | |
| 48 | .. note:: that some CodeIgniter classes such as Sessions require Active |
| 49 | Records be enabled to access certain functionality. |
| 50 | |
| 51 | Explanation of Values: |
| 52 | ---------------------- |
| 53 | |
| 54 | - **hostname** - The hostname of your database server. Often this is |
| 55 | "localhost". |
| 56 | - **username** - The username used to connect to the database. |
| 57 | - **password** - The password used to connect to the database. |
| 58 | - **database** - The name of the database you want to connect to. |
| 59 | - **dbdriver** - The database type. ie: mysql, postgres, odbc, etc. |
| 60 | Must be specified in lower case. |
| 61 | - **dbprefix** - An optional table prefix which will added to the table |
| 62 | name when running :doc:`Active Record <active_record>` queries. This |
| 63 | permits multiple CodeIgniter installations to share one database. |
| 64 | - **pconnect** - TRUE/FALSE (boolean) - Whether to use a persistent |
| 65 | connection. |
| 66 | - **db_debug** - TRUE/FALSE (boolean) - Whether database errors should |
| 67 | be displayed. |
| 68 | - **cache_on** - TRUE/FALSE (boolean) - Whether database query caching |
| 69 | is enabled, see also :doc:`Database Caching Class <caching>`. |
| 70 | - **cachedir** - The absolute server path to your database query cache |
| 71 | directory. |
| 72 | - **char_set** - The character set used in communicating with the |
| 73 | database. |
| 74 | - **dbcollat** - The character collation used in communicating with the |
| 75 | database. |
| 76 | |
| 77 | .. note:: For MySQL and MySQLi databases, this setting is only used |
| 78 | as a backup if your server is running PHP < 5.2.3 or MySQL < 5.0.7 |
| 79 | (and in table creation queries made with DB Forge). There is an |
| 80 | incompatibility in PHP with mysql_real_escape_string() which can |
| 81 | make your site vulnerable to SQL injection if you are using a |
| 82 | multi-byte character set and are running versions lower than these. |
| 83 | Sites using Latin-1 or UTF-8 database character set and collation are |
| 84 | unaffected. |
| 85 | |
| 86 | - **swap_pre** - A default table prefix that should be swapped with |
| 87 | dbprefix. This is useful for distributed applications where you might |
| 88 | run manually written queries, and need the prefix to still be |
| 89 | customizable by the end user. |
| 90 | - **autoinit** - Whether or not to automatically connect to the |
| 91 | database when the library loads. If set to false, the connection will |
| 92 | take place prior to executing the first query. |
| 93 | - **stricton** - TRUE/FALSE (boolean) - Whether to force "Strict Mode" |
| 94 | connections, good for ensuring strict SQL while developing an |
| 95 | application. |
| 96 | - **port** - The database port number. To use this value you have to |
| 97 | add a line to the database config |
| 98 | array.:: |
| 99 | |
| 100 | $db['default']['port'] = 5432; |
| 101 | |
| 102 | |
| 103 | .. note:: Depending on what database platform you are using (MySQL, |
| 104 | Postgres, etc.) not all values will be needed. For example, when using |
| 105 | SQLite you will not need to supply a username or password, and the |
| 106 | database name will be the path to your database file. The information |
| 107 | above assumes you are using MySQL. |