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

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);
}

Related

Loading databases dynamically in codeigniter

My application uses Codeigniter 3.X and I'm trying to use different DBs for each of my user.
So the main db is main_db and if a user with user id 1 logs in then I want to use db_1 and so on.
Codeigniter allows me to use multiple DBs but I want to achieve this dynamically.
Is there a way to load DBs dynamically in codeigniter? If yes then how?
I have included my database.php below and I load the database in model construct as
$this->load->database();
database.php
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'username',
'password' => 'password',
'database' => 'main_db',
'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
);

Codeigniter no connect in database

My error >>>
An uncaught Exception was encountered
Type: ParseError
Message: syntax error, unexpected '<', expecting end of file
Filename: C:\wamp64\www\application\config\database.php
Line Number: 97
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'sistema', // your database name
'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
);
?>
complete code https://pastebin.com/us3mycVT
Remove the ?> at the end of that file.
Here you can find more information on why you shouldn't close the php tags on those files.
Why would one omit the close tag?
Looked at the original file sent by the user. Basically there was a hidden character in the file. Created a new database.php with original encoding and everything worked fine.

CodeIgniter Null result set from Mysql DB query

I am updating an existing CI project on an AWS Ubuntu 12/PHP 4.2/CI 1.3.1 to a new AWS instance running Ubuntu 16.0.4 LTS/Apache 2.4.18/PHP 7.0.8/MySQL 5.7.15/CI 3.1.0.
I can connect and select data from mysql command line using the settings in database.php I wrote a non-CI test PHP page that also returns correct results. The same query in the model returns 0 results.
I've been tweaking the settings trying to get this to work. This is the current snapshot of database.php.
$active_group = 'default';
$query_builder = TRUE;
// 'db_debug' => (ENVIRONMENT !== 'production'),
$db['default'] = array(
'dsn' => '',
'hostname' => '127.0.0.1',
'username' => '****',
'password' => '****',
'database' => '****',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'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
);
Here is the applicable section of the model:
class User_model extends CI_Model
{
function __construct()
{
parent::__construct();
log_message("info","User_model class instantiated");
}
... section omitted ...
function login($username, $password)
{
$this->load->library('encrypt');
// Get the hash of the entered password to lookup in the database
//$password_hash = $this->encrypt->sha1($password); (Removed 10/8/2016 ES SHA1 deprecated)
$newpassword_hash = $this->encrypt->encode($password);
$this->db->where('user_name', $username );
$this->db->or_where('email', $username);
$query = $this->db->get('users', 1);
if($query->num_rows == 0){
log_message("info","User not found");
return false;
}
else
{
... remainder omitted ...
var_dump($this->db->last_query());
string(107) "SELECT * FROM `users` WHERE `user_name` = 'xxxxxx#gmail.com' OR `email` = 'xxxxxx#gmail.com' LIMIT 1"
var_dump($query);
object(CI_DB_mysqli_result)#23 (8) { ["conn_id"]=> object(mysqli)#15 (19) { ["affected_rows"]=> int(1) ["client_info"]=> string(79) "mysqlnd 5.0.12-dev - 20150407 - $Id: 241ae00989d1995ffcbbf63d579943635faf9972 $" ["client_version"]=> int(50012) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["field_count"]=> int(60) ["host_info"]=> string(20) "127.0.0.1 via TCP/IP" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(23) "5.7.15-0ubuntu0.16.04.1" ["server_version"]=> int(50715) ["stat"]=> string(138) "Uptime: 229367 Threads: 7 Questions: 1083 Slow queries: 0 Opens: 440 Flush tables: 1 Open tables: 241 Queries per second avg: 0.004" ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(223) ["warning_count"]=> int(0) } ["result_id"]=> object(mysqli_result)#22 (5) { ["current_field"]=> int(0) ["field_count"]=> int(60) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) } ["result_array"]=> array(0) { } ["result_object"]=> array(0) { } ["custom_result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows"]=> NULL ["row_data"]=> NULL }
if you use $query = $this->db->get('users', 1); this format to query in CI
then the number of rows will be achieved by as following:
$this->db->where('user_name', $username );
$this->db->or_where('email', $username);
$query = $this->db->get('users')->result_array();
if(count($query) == 0){
log_message("info","User not found");
return false;
}
else{
//do what you want
}
otherwise you can use like the following:
$query = $this->db->query('your sql here');
if($query->num_rows == 0){
log_message("info","User not found");
return false;
}
else{
//do what you want
}
Hope this may solve your problem.:)

Code Igniter : A Database Error Occurred SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

I'm trying to upload a website to the server. Code Igniter is framework that I 've used for this site and I'm using CI for the first time. While trying to upload the website to the server with following database setiings:
$db['default'] = array(
'dsn' => 'mysql:host=localhost;dbname=database_name',
'hostname' => 'mysql:host=localhost',
'username' => 'database_user',
'password' => 'database_password',
'database' => 'database_name',
'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
);
I've been getting errors as follows and I tried to search the net for solving my issues. I found some issues which were similar to mine but I can't figure out what I should be doing because most of the issues were for local servers. Please could someone help me out to solve it thanks.
A Database Error Occurred
SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
Filename: controllers/Home.php
Line Number: 9
A PHP Error was encountered
Severity: Warning
Message: PDO::__construct(): The server requested authentication method unknown to the client [mysql_old_password]
Filename: pdo/pdo_driver.php
Line Number: 133
Backtrace:
Edit:
Try:
$db['default'] = array(
'hostname' => 'localhost',
'username' => 'user',
'password' => 'password',
'database' => 'database_name',
'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
);
or
$db['default'] = array(
'dsn' => 'mysql:host=localhost;dbname=codeigniter',
'username' => 'codeigniter',
'password' => 'codeigniter',
'database' => 'codeigniter',
'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
);
You can use this code for database setting in database.php
$active_group = 'live';
$active_record = TRUE;
$db['live']['hostname'] = 'localhost';
$db['live']['username'] = 'user';
$db['live']['password'] = 'password';
$db['live']['database'] = 'database';
$db['live']['dbdriver'] = 'pdo';
$db['live']['dbprefix'] = '';
$db['live']['pconnect'] = TRUE;
$db['live']['db_debug'] = TRUE;
$db['live']['cache_on'] = FALSE;
$db['live']['cachedir'] = '';
$db['live']['char_set'] = 'utf8';
$db['live']['dbcollat'] = 'utf8_general_ci';
$db['live']['swap_pre'] = '';
$db['live']['autoinit'] = TRUE;
$db['live']['stricton'] = FALSE;
Set $config["useDatabaseConfig"] = true; in your config.php
I hope it will work for you.

Codeigniter - multiple database connections

I have to retrieve a MySQL database information from master database and then connect to that database, and fetch some records.
I mean that holding one database I want to load another database.
Is it possible with Codeigniter? Right now I'm using following lines of code in my model.
function connectDb($credential)
{
$config['hostname'] = $credential['server'];
$config['username'] = $credential['username'];
$config['password'] = $credential['password'];
$config['database'] = $credential['database'];
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
$DB2=$this->load->database($config);
$DB2->db->select('first_name,last_name');
$query = $DB2->db->get('person');
print_r($query);
}
its not working is there any other way?
You should provide the second database information in `application/config/database.php´
Normally, you would set the default database group, like so:
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "database_name";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$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;
Notice that the login information and settings are provided in the array named $db['default'].
You can then add another database in a new array - let's call it 'otherdb'.
$db['otherdb']['hostname'] = "localhost";
$db['otherdb']['username'] = "root";
$db['otherdb']['password'] = "";
$db['otherdb']['database'] = "other_database_name";
$db['otherdb']['dbdriver'] = "mysql";
$db['otherdb']['dbprefix'] = "";
$db['otherdb']['pconnect'] = TRUE;
$db['otherdb']['db_debug'] = FALSE;
$db['otherdb']['cache_on'] = FALSE;
$db['otherdb']['cachedir'] = "";
$db['otherdb']['char_set'] = "utf8";
$db['otherdb']['dbcollat'] = "utf8_general_ci";
$db['otherdb']['swap_pre'] = "";
$db['otherdb']['autoinit'] = TRUE;
$db['otherdb']['stricton'] = FALSE;
Now, to actually use the second database, you have to send the connection to another variable that you can use in your model:
function my_model_method()
{
$otherdb = $this->load->database('otherdb', TRUE); // the TRUE paramater tells CI that you'd like to return the database object.
$query = $otherdb->select('first_name, last_name')->get('person');
var_dump($query);
}
That should do it.
The documentation for connecting to multiple databases can be found here: http://codeigniter.com/user_guide/database/connecting.html
The best way is to use different database groups. If you want to keep using the master database as usual ($this->db) just turn off persistent connexion configuration option to your secondary database(s). Only master database should work with persistent connexion :
Master database
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "database_name";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$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;
Secondary database (notice pconnect is set to false)
$db['otherdb']['hostname'] = "localhost";
$db['otherdb']['username'] = "root";
$db['otherdb']['password'] = "";
$db['otherdb']['database'] = "other_database_name";
$db['otherdb']['dbdriver'] = "mysql";
$db['otherdb']['dbprefix'] = "";
$db['otherdb']['pconnect'] = FALSE;
$db['otherdb']['db_debug'] = FALSE;
$db['otherdb']['cache_on'] = FALSE;
$db['otherdb']['cachedir'] = "";
$db['otherdb']['char_set'] = "utf8";
$db['otherdb']['dbcollat'] = "utf8_general_ci";
$db['otherdb']['swap_pre'] = "";
$db['otherdb']['autoinit'] = TRUE;
$db['otherdb']['stricton'] = FALSE;
Then you can use secondary databases as database objects while using master database as usual :
// use master dataabse
$users = $this->db->get('users');
// connect to secondary database
$otherdb = $this->load->database('otherdb', TRUE);
$stuff = $otherdb->get('struff');
$otherdb->insert_batch('users', $users->result_array());
// keep using master database as usual, for example insert stuff from other database
$this->db->insert_batch('stuff', $stuff->result_array());
Use this.
$dsn1 = 'mysql://user:password#localhost/db1';
$this->db1 = $this->load->database($dsn1, true);
$dsn2 = 'mysql://user:password#localhost/db2';
$this->db2= $this->load->database($dsn2, true);
$dsn3 = 'mysql://user:password#localhost/db3';
$this->db3= $this->load->database($dsn3, true);
Usage
$this->db1 ->insert('tablename', $insert_array);
$this->db2->insert('tablename', $insert_array);
$this->db3->insert('tablename', $insert_array);
It works fine for me(Codeigniter 3)...
This is default database :
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'mydatabase',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'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
);
Add another database at the bottom of database.php file
$db['second'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'mysecond',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'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 autoload.php config file
$autoload['libraries'] = array('database', 'email', 'session');
The default database is worked fine by autoload the database library
but second database load and connect by using constructor in model and
controller...
<?php
class Seconddb_model extends CI_Model {
function __construct(){
parent::__construct();
//load our second db and put in $db2
$this->db2 = $this->load->database('second', TRUE);
}
public function getsecondUsers(){
$query = $this->db2->get('members');
return $query->result();
}
}
?>
While looking at your code, the only thing I see wrong, is when you try to load the second database:
$DB2=$this->load->database($config);
When you want to retrieve the database object, you have to pass TRUE in the second argument.
From the Codeigniter User Guide:
By setting the second parameter to TRUE (boolean) the function will
return the database object.
So, your code should instead be:
$DB2=$this->load->database($config, TRUE);
That will make it work.
default database
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'test',
'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
);
another db
$db['database2'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'test_2', // Database name
'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 the model you have to call
public function getRecords(){
// Load database
$db2 = $this->load->database('database2', TRUE);
// Select records from 1st database
$this->db->select('*');
$query = $this->db->get('record1');
$result1 = $query->result_array();
// Select records from 2nd database
$db2->select('*');
$query = $db2->get('record2');
$result2 = $query->result_array();
$response = array("response1"=>$result1,"response2"=>$result2);
return $response;
}
If you need to connect to more than one database simultaneously you can do so as follows:
$DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);
Note: Change the words “group_one” and “group_two” to the specific group names you are connecting to (or you can pass the connection values as indicated above).
By setting the second parameter to TRUE (boolean) the function will return the database object.
Visit https://www.codeigniter.com/userguide3/database/connecting.html for further information.
As a supplement to the other answers, if you want to change the active database group within the model itself, so not needing to use another database connection, use the following within a model class:
public function set_database($database)
{
$CI =& get_instance();
$CI->db = null;
$this->load->database($database);
}
After calling from a controller, like:
$this->my_model->set_database('other_db');
You can then use your other model methods acting upon the newly specified database group, like:
$this->my_model->insert($data);
Note: Currently, only tested with codeigniter 3.