blob: 64adc3047e61b3ce0c3c56c32c38b43dd90310c6 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001###########################
2Connecting to your Database
3###########################
4
5There are two ways to connect to a database:
6
7Automatically Connecting
8========================
9
10The "auto connect" feature will load and instantiate the database class
11with every page load. To enable "auto connecting", add the word database
12to the library array, as indicated in the following file:
13
14application/config/autoload.php
15
16Manually Connecting
17===================
18
19If only some of your pages require database connectivity you can
20manually connect to your database by adding this line of code in any
21function where it is needed, or in your class constructor to make the
22database available globally in that class.
23
24::
25
26 $this->load->database();
27
28If the above function does **not** contain any information in the first
29parameter it will connect to the group specified in your database config
30file. For most people, this is the preferred method of use.
31
32Available Parameters
33--------------------
34
35#. The database connection values, passed either as an array or a DSN
36 string.
37#. TRUE/FALSE (boolean). Whether to return the connection ID (see
38 Connecting to Multiple Databases below).
39#. TRUE/FALSE (boolean). Whether to enable the Active Record class. Set
40 to TRUE by default.
41
42Manually Connecting to a Database
43---------------------------------
44
45The first parameter of this function can **optionally** be used to
46specify a particular database group from your config file, or you can
47even submit connection values for a database that is not specified in
48your config file. Examples:
49
50To choose a specific group from your config file you can do this::
51
52 $this->load->database('group_name');
53
54Where group_name is the name of the connection group from your config
55file.
56
57To connect manually to a desired database you can pass an array of
58values::
59
60 $config['hostname'] = "localhost"; $config['username'] = "myusername"; $config['password'] = "mypassword"; $config['database'] = "mydatabase"; $config['dbdriver'] = "mysql"; $config['dbprefix'] = ""; $config['pconnect'] = FALSE; $config['db_debug'] = TRUE; $config['cache_on'] = FALSE; $config['cachedir'] = ""; $config['char_set'] = "utf8"; $config['dbcollat'] = "utf8_general_ci"; $this->load->database($config);
61
62For information on each of these values please see the :doc:`configuration
63page <configuration>`.
64
65.. note:: For the PDO driver, $config['hostname'] should look like
66 this: 'mysql:host=localhost'
67
68Or you can submit your database values as a Data Source Name. DSNs must
69have this prototype::
70
71 $dsn = 'dbdriver://username:password@hostname/database'; $this->load->database($dsn);
72
73To override default config values when connecting with a DSN string, add
74the config variables as a query string.
75
76::
77
78 $dsn = 'dbdriver://username:password@hostname/database?char_set=utf8&dbcollat=utf8_general_ci&cache_on=true&cachedir=/path/to/cache'; $this->load->database($dsn);
79
80Connecting to Multiple Databases
81================================
82
83If you need to connect to more than one database simultaneously you can
84do so as follows::
85
86 $DB1 = $this->load->database('group_one', TRUE); $DB2 = $this->load->database('group_two', TRUE);
87
88Note: Change the words "group_one" and "group_two" to the specific
89group names you are connecting to (or you can pass the connection values
90as indicated above).
91
92By setting the second parameter to TRUE (boolean) the function will
93return the database object.
94
Joseph Wensleyf24f4042011-10-06 22:53:29 -040095.. note:: When you connect this way, you will use your object name to issue
96 commands rather than the syntax used throughout this guide. In other
97 words, rather than issuing commands with:
98
99 |
100 | $this->db->query();
101 | $this->db->result();
102 | etc...
103 |
104 | You will instead use:
105 |
106 | $DB1->query();
107 | $DB1->result();
108 | etc...
Derek Jones8ede1a22011-10-05 13:34:52 -0500109
110Reconnecting / Keeping the Connection Alive
111===========================================
112
113If the database server's idle timeout is exceeded while you're doing
114some heavy PHP lifting (processing an image, for instance), you should
115consider pinging the server by using the reconnect() method before
116sending further queries, which can gracefully keep the connection alive
117or re-establish it.
118
119::
120
121 $this->db->reconnect();
122
123Manually closing the Connection
124===============================
125
126While CodeIgniter intelligently takes care of closing your database
127connections, you can explicitly close the connection.
128
129::
130
131 $this->db->close();
132