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