CI code that operates on multiple databases in php

  • 2020-05-17 05:02:35
  • OfStack

Actually, it's not that difficult, because I just started CI, so I still took a lot of trouble. Fortunately, there is a manual.

Find the database configuration file and add the connection information for 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 TRUE to return to connect ID and enable ActionRecord class, respectively
Then you can use $this- in the controller > xx- > query() to query the data of your other library. You don't need to create an modle file for the other one
 
<?php 
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 perform the XX The library sql the  
$this->xx->query($sql); 
} 
?> 

Related articles: