CodeIgniter Laravel PHP Example HTML Javascript jQuery MORE
Need a Website Or Web Application.Contact : +91 9437911966 (Whatsapp) Note: Paid Service

Connect to a database in codeigniter


In codeigniter there are two ways to connect with database:

  1. Automatically connect
  2. Manually connect

Automatically connect:

The “auto connect” feature will load and instantiate the database class with every page load. To enable “auto connecting”, add the word database to the library array in autoload.php file.

as per the below code:

/*
| -------------------------------------------------------------------
|  Auto-load Libraries
| -------------------------------------------------------------------
| These are the classes located in system/libraries/ or your
| application/libraries/ directory, with the addition of the
| 'database' library, which is somewhat of a special case.
|
| Prototype:
|
|	$autoload['libraries'] = array('database', 'email', 'session');
|
| You can also supply an alternative library name to be assigned
| in the controller:
|
|	$autoload['libraries'] = array('user_agent' => 'ua');
*/
$autoload['libraries'] = array('database');

Manually Connect:

If you want to load the database in some pages you can connect by adding the below line:

$this->load->database();

You can add under the class constructor to access the database globally for that particular class.

Connecting to Multiple Databases

In codeigniter it is possible to connect more than one database.

$DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);