Connecting codeigniter to mysql and oracle in the same application Connecting codeigniter to mysql and oracle in the same application codeigniter codeigniter

Connecting codeigniter to mysql and oracle in the same application


I worked with oracle and mysql using Codeigniter.

You used $this->load->database('oracle',true); this should be assigned to a variable as you used 2nd parameter true.like this

$oracle_db=$this->load->database('oracle',true);//connected with oracle$mysql_db=$this->load->database('default',true);//connected with mysql

Now you can use these two variables for your query.Like

$oracle_db->get('people')->result();

or

$mysql_db->get('people')->result();

So finally your model should be like this(do not load database at your controller)

function __construct()//model construct function{    parent::__construct();    $this->oracle_db=$this->load->database('oracle',true);    $this->mysql_db=$this->load->database('default',true);}function getPeople(){   return $this->oracle_db->get('people')->result();}

Hope you will understand. Make sure it connects with your oracle db.

My database.php for oracle was like this

$tns = "(DESCRIPTION =    (ADDRESS_LIST =      (ADDRESS = (PROTOCOL = TCP)(HOST = YOUR_IP)(PORT = 1521))    )    (CONNECT_DATA =      (SID = YOUR_SID)    )  )       ";$db['oracle']['hostname'] = $tns;


You have to load the database and have to use that object to query from database

$oracle =  $this->load->database('oracle',true);$query = $oracle->query("SELECT * FROM people");

and change the pconnect flag to false as CI have issues maintaining the persistent connection to multiple database.


Try like this it works for me

$active_group = 'oracle';$query_builder = TRUE;$db['oracle'] = array(    'dsn'   => '',    'hostname' => '192.168.0.246:1521/orcl',    //'hostname' => 'localhost',    'username' => 's_dev0101',     'password' => 's_dev0101',    'database' => 'testdb',    'dbdriver' => 'oci8',    '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,    'save_queries' => TRUE,);$active_group = 'default';$query_builder = TRUE;$db['default'] = array(    'dsn'   => '',    'hostname' => '192.168.0.106', //192.168.0.106    //'hostname' => 'localhost',    'username' => 'aaa',    'password' => 'aaa',    'database' => 'ttttt',    '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);

In Model:

public $db;    function __construct()    {        parent::__construct();        $this->db = $this->load->database('default',true);//connected with mysql        $oracle_db = $this->load->database('oracle',true);//connected with oracle        var_dump($oracle_db);    }