CodeIgniter: Multiple Databases - Accessing database config in a second database
From the docs ( https://www.codeigniter.com/user_guide/database/connecting.html ) :
The first parameter of this function can optionally be used to specify
a particular database group from your config file, or you can even
submit connection values for a database that is not specified in your
config file.
So you would do something like this, replacing the values with values from the master database:
$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$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";
$this->load->database($config);
If you need to maintain a connection to the master database and the customer database, then change the last line to:
$customer_db = $this->load->database($config, TRUE);
// to use the master database:
$this->db->query("SELECT * FROM my_table");
// to then use the customer database:
$customer_db->query("SELECT * FROM whatever");
Codeigniter: Different methods with different database connection (multiple database connection)
You need to define variable outside you constructor
public $DB1;
public $DB2;
And call it inside constructor as
function __construct()
{
parent::__construct();
$this->DB1= $this->load->database("db1", TRUE);
$this->DB2= $this->load->database("db2", TRUE);
}
And inside your function
public function get_from_db1($id)
{
// $query = $this->db->get_where("my_table1",array("ID"=>$id));
$query = $this->DB1->get_where("my_table1", array("ID" => $id));
return $query->row_array();
}
public function get_from_db2($id)
{
// $query = $this->db->get_where("my_table2",array("ID"=>$id));
$query = $this->DB2->get_where("my_table2", array("ID" => $id));
return $query->row_array();
}
Codeigniter Multiple Database connections
In order to return the database object, you need to pass a TRUE as second paramenter:
$db2 = $this->load->database('second', TRUE);
See the manual on database class for more info.
Make also sure you've loaded the config for that database in application/config/database.php
$db['default']['hostname'] = 'localhost';
//.........
$db['second']['hostname'] = 'localhost';
//..........
Queries on multiple database with codeIgniter
You can setup 2 database in config/database.php file
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'first_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
);
//set second db configuration
$db['otherdb'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'second_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
);
When you want use default database means master database
// use master dataabse
$users = $this->db->get('users');
// connect to secondary database
$otherdb = $this->load->database('otherdb', TRUE);
$data = $otherdb->get('table_name');
if your first db name is base1 and second is base2
$this->db->select('table1.item1 FROM table1');
$this->db->from('table1');
$this->db->join('base2.table3', 'base2.table3.item2 =table1.item2');
$this->where('base2.table3.item4','toto')
$query = $this->db->get();
Codeigniter - Using Multiple Databases
Instead of applying the hack as mentioned by Camacho you can also set the 'pconnect'-flag in the database.php file to FALSE for all connections.
Using Multiple Databases Within CodeIgniter
in your database config file add as many configuration groups as the numbers of your databases:
$db['a']['hostname'] = 'localhost';
$db['a']['username'] = 'user';
$db['a']['password'] = 'pw';
$db['a']['database'] = 'db1';
...
$db['b']['hostname'] = 'localhost';
$db['b']['username'] = 'user';
$db['b']['password'] = 'pw';
$db['b']['database'] = 'db2';
...
//set the default db
$active_group = 'a';
then on your model initialize a class variable:
private $db_b;
and, into the contructor, set it as follow
__construct()
{
...
$this->db_b = $this->load->database('b', TRUE);
}
now you are able to use the database b
as usual:
$this->db_b->query('YOUR QUERY');
and obviously the default one as follow:
$this->db->query('YOUR QUERY');
codeigniter connect two database
I found the answer.
function simple_query($sql)
{
if ( ! $this->conn_id)
{
$this->initialize();
}
$this->db_select(); // Add this code
return $this->_execute($sql);
}
Reference here
PHP Codeigniter using Multiple Databases - is to many connections
I made this myself. I create library:
<?php
class Databaseloader {
public function __construct() {
$this->load();
}
public function load() {
$CI = &get_instance();
$CI->db = $CI->load->database('reader_instance', TRUE);
$CI->db2 = $CI->load->database('writer_instance', TRUE);
}
}
Next in autoload.php replaced in variable $autoload['libraries'] database with databaseloader and remove load database in each models.
$autoload['libraries'] = array('databaseloader');
Related Topics
Getting the First Character of a String with $Str[0]
PHP Include Best Practices Question
Facebook Connect- $User_Id = $Facebook->Getuser(); Always Returning 0
Use PHP Composer to Clone Git Repo
Access Post Values in Symfony2 Request Object
PHP Flush That Works... Even in Nginx
Cakephp-3.X: How to Change the Data Type of a Selected Alias
Find First Character That Is Different Between Two Strings
Sqlstate[Hy000] [1698] Access Denied for User 'Root'@'Localhost'
How to Add Some Textfield in Form Register (Laravel Generator Infyom)
Php: Create an Array for a Range
How Follow the Don't Repeat Yourself Principle When Consuming My Own Laravel API
Redirect After Login on Wordpress
PHP Share Variable Among Different Users/Sessions
How to Pass Arguments to My Function Through Add_Action