Loading...

codeigniter tutorial

CodeIgniter Multi Database Connection 

database.php

<?php
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
	'dsn'	=> 'pgsql:host=123.123.123.123;dbname=books',
	'hostname' => '123.123.123.123',
	'username' => 'postgres',
	'password' => 'post123',
	'database' => 'books',
	'dbdriver' => 'pdo',
	'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
);
$active_group = 'DEMO';
$query_builder = TRUE;
$db['DEMO'] = array(
    'dsn'	=> 'pgsql:host=localhost;dbname=pens',
    'hostname' => 'localhost',
    'username' => 'postgres',
    'password' => 'post123',
    'database' => 'pens',
    'dbdriver' => 'pdo',
    '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
);
$active_group = 'DEMO2';
$query_builder = TRUE;
$db['DEMO2'] = array(
    'dsn'	=> 'pgsql:host=localhost;dbname=mobiles',
    'hostname' => 'localhost',
    'username' => 'postgres',
    'password' => 'post123',
    'database' => 'mobiles',
    'dbdriver' => 'pdo',
    '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
);

autoload.php

<?php
$autoload['libraries'] = array('database', 'email', 'session');
$autoload['helper'] = array('url', 'file');
$autoload['model'] = array('Demo_model');

Demo_controller.php

<?php
class Demo_controller extends CI_Controller
{
public function __construct()
    {
        parent::__construct();
    }
    
    //emp list
    public function emp()
    {
     $this->Demo_model->new_req_details();
    }
    
    //penslist
     public function new_pens()
    {
     $this->Demo_model->new_pens();
    }
    
    //books list
     public function new_books()
    {
     $this->Demo_model->new_books();
    }
}

Demo_model.php

<?php
class Demo_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
        //error_reporting(0);
        $this->db1 = $this->load->database('default', TRUE);
        $this->db2 = $this->load->database('DEMO', TRUE);
          $this->db3=$this->load->database('DEMO1', TRUE);
    }
      public function new_req_details()
    {
        $this->db1->select("*");
        $this->db1->from("employee_info");
        $this->db1->where("pmt06_emp_code", 123);
        $query = $this->db1->get();
        $this->db1->close();
        return $query->result();
    }
      public function new_books()
    {
        $this->db2->select("*");
        $this->db2->from("books2019");
        $this->db2->where("book_name",'naruto');
        $query = $this->db2->get();
        $this->db2->close();
        return $query->result();
    }
      public function new_pens()
    {
        $this->db3->select("*");
        $this->db3->from("pens");
       $query = $this->db3->get();
        $this->db3->close();
        return $query->result();
    }
 }