First, the configuration database
1. Description: Codeiginter has a configuration file that lets you store database connection values (Username: username, password: password, database name: database name, etc... ) 2. file Location: The configuration file is located in application/config/database.php3. Note: The key value for the Pconnect key name of the database other than the default database is false4. example: $active _group = ' default ' ; $active _record = true; $db [' Default '] [' Hostname '] = ' localhost '; the hostname of the //database, typically located on this machine, can be represented as "localhost" $DB [' Default '] [' username '] = ' root '; //user name to connect to the database $DB [' Default '] [' Password '] = ' 123456 '; //Login Database Password $DB [' Default '] [' database '] = ' db1 '; //you need to connect the name of the databases $DB [' Default '] [' dbdriver '] = ' MySQL '; //database type. For example: Mysql,postgres,odbc and so on. Must be in lowercase letters $db [' Default '] [' Dbprefix '] =&nbsP; "; //A data table prefix when running a active record query, which allows multiple CodeIgniter programs to be installed on a database connection $DB [' Default '] [' Pconnect '] = true; // true/false (Boolean)-Use persistent links $db [' Default '] [' Db_debug '] = true; //true/false ( Boolean)-Displays database error messages $db [' Default '] [' cache_on '] = false; //true/false (Boolean)-whether the database query cache is open. For details see CodeIgniter Database Cache class $db [' Default '] [' cachedir '] = '; Number Data base query cache directory server absolute path $db [' Default '] [' char_set '] = ' UTF8 '; //the character set used to communicate with the database $db [' Default '] [' Dbcollat '] = ' utf8_general _ci '; //the character rules used to communicate with the database $db [' Default '] [' swap_pre '] = ' '; //replaces the default Dbprefix table prefix, which is useful for a separate application, and you can use a table prefix that is customized by the end user in the query $db [' Default '] [' autoinit '] = true; //when the database (database Library) Whether it is necessary to automatically connect to the database when it is loaded, if set to false, will connect $db [' Default '] [' Stricton '] before first query] = false; //true/false (Boolean)-whether to force the use of the "Strict mode" link, using strict when developing the program SQL is a good habit $db [' DB2 '] [' hostname '] = ' localhost '; $db [' DB2 '] [' username '] = ' root '; $db [' DB2 '] [' Password '] = 123456 '; $db [' DB2 '] [' Database '] = ' DB2 '; $db [' DB2 '] [' dbdriver ' ] = ' MySQL '; $db [' DB2 '] [' dbprefix '] = '; $ db[' DB2 ' [' Pconnect '] = false; //special attention Oh $db [' DB2 '] [' db_ Debug '] = true; $db [' DB2 '] [' cache_on '] = false; $DB [' DB2 '] [' CACHedir '] = '; $db [' DB2 '] [' char_set '] = ' UTF8 '; $DB [' DB2 '] [' Dbcollat '] = ' utf8_general_ci '; $db [' DB2 '] [' swap_pre '] = '; $db [' DB2 '] [' autoinit '] = true; $db [' DB2 ' [' Stricton '] = false;
Second, connect the database in model
1. example:class test_model extends ci_model{ var $db _connect1; var $db _connect2; public function __construct () { parent::__construct (); $this->db_connect1 = $this->load->database (' Default ', true); $this->db_connect2= $this->load->database (' DB2 ', TRUE); } public function get_db1 ($date) { $query = $this->db_connect1->query ("select * From db1 where date= ' $date '); return $ Query->row_array ();     }    PUBLIC FUNCTION GET_DB2 ($date ) {    &NBsp; $query = $this->db_connect2->query ("Select * from db2 where date= ' $date '); return $query->row_ Array (); }}
CodeIgniter Framework connects multiple databases at the same time