Connecting to your Database

You can connect to your database by adding this line of code in anyfunction where it is needed, or in your class constructor to make thedatabase available globally in that class.

  1. $db = \Config\Database::connect();

If the above function does not contain any information in the firstparameter it will connect to the default group specified in your database configfile. For most people, this is the preferred method of use.

A convenience method exists that is purely a wrapper around the above lineand is provided for your convenience:

  1. $db = db_connect();

Available Parameters

  • The database group name, a string that must match the config class’ property name. Default value is $config->defaultGroup.
  • TRUE/FALSE (boolean). Whether to return the shared connection (seeConnecting to Multiple Databases below).

Manually Connecting to a Database

The first parameter of this function can optionally be used tospecify a particular database group from your config file. Examples:

To choose a specific group from your config file you can do this:

  1. $db = \Config\Database::connect('group_name');

Where group_name is the name of the connection group from your configfile.

Multiple Connections to Same Database

By default, the connect() method will return the same instance of thedatabase connection every time. If you need to have a separate connectionto the same database, send false as the second parameter:

  1. $db = \Config\Database::connect('group_name', false);

Connecting to Multiple Databases

If you need to connect to more than one database simultaneously you cando so as follows:

  1. $db1 = \Config\Database::connect('group_one');
  2. $db = \Config\Database::connect('group_two');

Note: Change the words “group_one” and “group_two” to the specificgroup names you are connecting to.

Note

You don’t need to create separate database configurations if youonly need to use a different database on the same connection. Youcan switch to a different database when you need to, like this:

$db->setDatabase($database2_name);

Connecting with Custom Settings

You can pass in an array of database settings instead of a group name to geta connection that uses your custom settings. The array passed in must bethe same format as the groups are defined in the configuration file:

  1. $custom = [
  2. 'DSN' => '',
  3. 'hostname' => 'localhost',
  4. 'username' => '',
  5. 'password' => '',
  6. 'database' => '',
  7. 'DBDriver' => 'MySQLi',
  8. 'DBPrefix' => '',
  9. 'pConnect' => false,
  10. 'DBDebug' => (ENVIRONMENT !== 'production'),
  11. 'cacheOn' => false,
  12. 'cacheDir' => '',
  13. 'charset' => 'utf8',
  14. 'DBCollat' => 'utf8_general_ci',
  15. 'swapPre' => '',
  16. 'encrypt' => false,
  17. 'compress' => false,
  18. 'strictOn' => false,
  19. 'failover' => [],
  20. 'port' => 3306,
  21. ];
  22. $db = \Config\Database::connect($custom);

Reconnecting / Keeping the Connection Alive

If the database server’s idle timeout is exceeded while you’re doingsome heavy PHP lifting (processing an image, for instance), you shouldconsider pinging the server by using the reconnect() method beforesending further queries, which can gracefully keep the connection aliveor re-establish it.

Important

If you are using MySQLi database driver, the reconnect() methoddoes not ping the server but it closes the connection then connects again.

  1. $db->reconnect();

Manually closing the Connection

While CodeIgniter intelligently takes care of closing your databaseconnections, you can explicitly close the connection.

  1. $db->close();