CodeIgniter Database Configuration

In continuation of basic configuration tutorial, now we are going to configure our database for the web application. The database is most important (Backbone) part of any application for storing and retrieve information an organised manner. In CodeIgniter database configuration comes in database.php under /application/config directory. CodeIgniter currently supports following database to build any web application:

  • MySQL (5.1+) via the mysql (deprecated), mysqli and pdo drivers
  • Oracle via the oci8 and pdo drivers
  • PostgreSQL via the postgre and pdo drivers
  • MS SQL via the mssql, sqlsrv (version 2005 and above only) and pdo drivers
  • SQLite via the sqlite (version 2), sqlite3 (version 3) and pdo drivers
  • CUBRID via the cubrid and pdo drivers
  • Interbase/Firebird via the ibase and pdo drivers
  • ODBC via the odbc and pdo drivers (you should know that ODBC is actually an abstraction layer)
 $db['default'] = array(
 'dsn' => '',
 'hostname' => 'localhost',
 'username' => '',
 'password' => '',
 'database' => '',
 'dbdriver' => 'mysqli',
 'dbprefix' => '',
 'pconnect' => FALSE,
 'db_debug' => (ENVIRONMENT !== 'production'),
 'cache_on' => FALSE,
 'cachedir' => '',
 'char_set' => 'utf8',
 'dbcollat' => 'utf8_general_ci',
 'swap_pre' => '',
 'encrypt' => FALSE,
 'compress' => FALSE,
 'stricton' => FALSE,
 'failover' => array(),
 'save_queries' => TRUE
);

As we see above code snippet there are lots of parameters uses in DB config as an array(). We need not change all those parameters. You can leave those parameters its default value, except hostname, username, password, database and dbdriver.

Hostname– In this parameter specify the database server location. If your database locates the same server where your application runs use “localhost” for hostname parameter. If your database server runs from the different server then you have to put Domain name or IP address of database server.

Username– In this parameter tell your database user name.

Password– In this parameter specify your database password.

Database– In this parameter specify your database name.

Dbdriver– In this parameter set your database type. For example MySQL, MySQLi, Postgre SQL, ODBC, and MS SQL etc.

 $db['default'] = array(
 'dsn' => '',
 'hostname' => 'localhost',
 'username' => 'dbuser',
 'password' => '1234',
 'database' => 'portal',
 'dbdriver' => 'mysqli',
 'dbprefix' => '',
 'pconnect' => FALSE,
 'db_debug' => FALSE,// Set FALSE if your application on production environment
 'cache_on' => FALSE,
 'cachedir' => '',
 'char_set' => 'utf8',
 'dbcollat' => 'utf8_general_ci',
 'swap_pre' => '',
 'encrypt' => FALSE,
 'compress' => FALSE,
 'stricton' => FALSE,
 'failover' => array(),
 'save_queries' => TRUE
 
);

 On above snippet, you notice that db_debug is set to FALSE. If your application is in a production environment then, it is recommended to set FALSE because of security issue. The db_debug determines whether database errors should be displayed or not.

$active_group —

Using $active_group variable you can choose a connection group to make active from sets of connection group. By default, there is only one group (the ‘default’ group). You can simply switch to different connection by changing the value of a variable as shown below −

 <?php
defined('BASEPATH') OR exit('No direct script access allowed');


//$active_group = 'default'; //comment on the defaule group

$active_group = 'conn2';   //Conn2 is active now

$query_builder = TRUE;

$db['default'] = array(
 'dsn' => '',
 'hostname' => 'localhost',
 'username' => 'dbuser',
 'password' => '1234',
 'database' => 'portal',
 'dbdriver' => 'mysqli',
 'dbprefix' => '',
 'pconnect' => FALSE,
 'db_debug' => FALSE,
 'cache_on' => FALSE,
 'cachedir' => '',
 'char_set' => 'utf8',
 'dbcollat' => 'utf8_general_ci',
 'swap_pre' => '',
 'encrypt' => FALSE,
 'compress' => FALSE,
 'stricton' => FALSE,
 'failover' => array(),
 'save_queries' => TRUE
 
);


$db['conn2'] = array(
 'dsn' => '',
 'hostname' => 'localhost',
 'username' => 'dbuser2',
 'password' => '1234',
 'database' => 'portal2',
 'dbdriver' => 'mysqli',
 'dbprefix' => '',
 'pconnect' => FALSE,
 'db_debug' => FALSE,
 'cache_on' => FALSE,
 'cachedir' => '',
 'char_set' => 'utf8',
 'dbcollat' => 'utf8_general_ci',
 'swap_pre' => '',
 'encrypt' => FALSE,
 'compress' => FALSE,
 'stricton' => FALSE,
 'failover' => array(),
 'save_queries' => TRUE
 
);

$query_builder —

The $query_builder variables determine whether or not to load the query builder class.

 

<<Previous                                                                                                     Next>>

The following two tabs change content below.

Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.

One Pingback

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.