How to switch database in codeigniter

I am new to CI and I just want to know if there is a way to switch databases to CI, for example: On the login page you will find a drop-down list with which you can specify which database you want to connect to, the view looks like this :

<select name="select" id="select" > <option value="1">DB1</option> <option value="2">DB2</option> </select> 

And I have 2 databases in database.php one is default another local by default I use by default.

$active_group = 'default'; $active_record = TRUE;

My question is how to specify db based on the dropdown value, for example, if I chose DB1, connect to the standard ones, DB2 will go to local ... I know how to swtich db manually:

  $this->load->database('default', TRUE); OR $this->load->database('local', TRUE); 

Since I have a different controller and model, how can I achieve this ... I really tried: first get the dropdownlist value in the login function:

 $this->load->model('My_Model'); $db = $this->input->post('select') $this->My_Model->getDB($db); 

and then in my model I got a function:

 function getDB($db) { if($db ==1) { $this->db = $this->load->database('default', TRUE); } elseif($db ==2) { $this->db = $this->load->database('local', TRUE); } } 

Unfortunately, it does not work ..... Any help would be greatly appreciated !!!!

+4
source share
1 answer

I have earned. First, I edited the database.php configuration file to include the new database. Then, in my model, I declared a class variable $current_db , and I wrote the following two functions:

 function getDB($db){ if($db ==1){ $this->current_db = $this->load->database('default', TRUE); } elseif($db ==2) { $this->current_db = $this->load->database('local', TRUE); } } function dyno_query($table, $id){ $query = $this->current_db->get_where($table, array('id' => $id)); return $query->result_array(); } 

Then in my controller, I ran the following:

 $arr = array(); $this->My_Model->getDB(1); $arr['default'] = $this->My_Model->dyno_query('default_table', 2); $this->My_Model->getDB(2); $arr['local'] = $this->My_Model->dyno_query('local_table', 74); var_dump($arr); 

The result was an array with data from both databases. Perhaps the key to this is to define a class variable with a name other than $ db.

EDIT:

To support loading the current database on loading each page, you need to use sessions. In the controller function that processes the outgoing data, you can enter something similar to the following:

 $db = $this->input->post('select') $this->My_Model->getDB($db); $this->session->set_userdata($db); 

In any controller that will use the database, you will want to add code to load the current database:

 function __construct(){ parent::__construct(); $this->load->model('My_Model'); $db = $this->session->userdata('db'); $this->My_Model->getDB($db); } 

EDIT:

If you need access to one database from different models, I suggest using the mddd library in EllisLab . Just create a PHP file called Db_manager.php followed by the code and move it to the application / library directory:

 class Db_manager { var $connections = array(); var $CI; function __construct() { $this->CI =& get_instance(); } function get_connection($db_name) { // connection exists? return it if (isset($this->connections[$db_name])) { return $this->connections[$db_name]; } else { // create connection. return it. $this->connections[$db_name] = $this->CI->load->database($db_name, true); return $this->connections[$db_name]; } } } 

In the constructor of each model that will use the database, add the following:

 var $current_db; function __construct(){ parent::__construct(); $this->load->library('Db_manager'); $db = $this->session->userdata('db'); if ($db == 1){ $this->current_db = $this->db_manager->get_connection('default'); } else { $this->current_db = $this->db_manager->get_connection('alternate'); } } 
+2
source

Source: https://habr.com/ru/post/1489076/


All Articles