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