Home  >  Article  >  Backend Development  >  Code for CI to operate multiple databases in php

Code for CI to operate multiple databases in php

高洛峰
高洛峰Original
2016-12-01 10:09:461005browse

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn