I'm using Mysql and Oracle for my CI application. I tried to connect it but I found that I cannot make a query to Oracle database. It always gave an error that the table is not exist.
I already set the database.php to something like this
$active_group = 'oracle';
$active_record = true;
$db['oracle']['hostname'] = '10.10.10.1:1521/ocidb';
$db['oracle']['username'] = 'ociuser';
$db['oracle']['password'] = 'ocipass';
$db['oracle']['database'] = 'ocidb';
$db['oracle']['dbdriver'] = 'oci8';
$db['oracle']['dbprefix'] = '';
$db['oracle']['pconnect'] = TRUE;
$db['oracle']['db_debug'] = FALSE;
$db['oracle']['cache_on'] = FALSE;
$db['oracle']['cachedir'] = '';
$db['oracle']['char_set'] = 'utf8';
$db['oracle']['dbcollat'] = 'utf8_general_ci';
$db['oracle']['swap_pre'] = '';
$db['oracle']['autoinit'] = TRUE;
$db['oracle']['stricton'] = FALSE;
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'mysqldb';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
In a controller, I want to query a table in oracle so I load it there.
function citizen(){
$this->load->database('oracle',true);
$data['someone'] = $this->people_model->getPeople();
$this->load->view('myview',$data);
}
And here is the people_model
function getPeople(){
return $this->db->get('people')->result();
}
When I run it, it will get error
Error Number: 1146
Table 'mysqldb.people' doesn't exist
SELECT * FROM (`people`) WHERE `id` = '21111'
It seems that it still makes query into the mysql, while the table people is in oracle. I also have tried to load the oracle database in model instead of in controller but same result.
How can I make a query to oracle in this case. Any answer would be appreciated. Thanks.
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);
}