Home > Article > Backend Development > Code for CI to operate multiple databases in php
Actually, this is not a difficult task. Because I just started CI, it still took a lot of trouble. Fortunately there is a manual.
Find the database configuration file and add the connection information of a new library. $config[XX].
In the controller,
a) $this->xx = $this->load->database('XX', TRUE) tip:XX represents the key name of the database information array you configured. It is recommended to set it later Is TRUE, which means returning the connection ID and enabling the ActionRecord class respectively b) Then in the controller you can use $this->xx->query() to query the data of another library of yours. There is no need to create a model file for your other library.
Copy the code. The code is as follows:
function __construct()
{
parent::__construct();
$this->xx = $this-> ;load->database('XX', TRUE);
$this->load->model('Default library table name');
}
function index()
{
//Now you can use $ this->xx->query() to execute the sql of XX library
$this->xx->query($sql);
}
?>