CodeIgniter configuration-database. php Usage example analysis

Source: Internet
Author: User
Tags pconnect
This article mainly introduces the database for CodeIgniter configuration. php Usage, combined with detailed analysis of the instance form, summarizes the common database connection methods of CodeIgniter. if you need a friend, refer to the example in this article to analyze the database of CodeIgniter configuration. php usage. We will share this with you for your reference. The details are as follows:

The database configuration file of CodeIgniter is located in application/config/database. php. This File defines the two-dimensional array of $ db. the reference file is as follows:

$active_group = 'default';$active_record = TRUE;$db['default']['hostname'] = 'localhost';$db['default']['username'] = 'root';$db['default']['password'] = '123456';$db['default']['database'] = 'test';$db['default']['dbdriver'] = 'mysql';$db['default']['dbprefix'] = '';$db['default']['pconnect'] = FALSE;$db['default']['db_debug'] = TRUE;$db['default']['cache_on'] = FALSE;$db['default']['cachedir'] = '';$db['default']['char_set'] = 'utf8';$db['default']['dbcollat'] = 'utf8_general_ci';$db['default']['swap_pre'] = '';$db['default']['autoinit'] = TRUE;$db['default']['stricton'] = FALSE;

Configuration instructions

The code is as follows:

Set session SQL _mode = "STRICT_ALL_TABLES"


How to connect to the database?

You can use the database method in Loader, that is, $ this-> load-> database (). The function is defined as follows:

/*** Database Loader ** @ param string indicates the Database connection value, which is transmitted by an array or DSN string. * @ Param bool whether to return the database object. Otherwise, the database object is assigned to the database attribute of the controller * @ param bool whether to use AR. the settings here overwrite the database. set * @ return object */function database ($ params = '', $ return = FALSE, $ active_record = NULL) in php ){}

$ Params has three values:

1. STRING. input the one-dimensional key name of the $ db array, such as default test. if it is null, the default value is $ active_group.

2. array: you can directly input a one-dimensional array similar to $ db, for example:

$this->load->database(array(  'hostname' => 'localhost',  'username' => 'root',  'password' => '123456',  'database' => 'test',  'dbdriver' => 'mysql',  'pconnect' => FALSE,  'db_debug' => TRUE,  'char_set' => 'utf8',  'dbcollat' => 'utf8_general_ci',));

3. DSN string, such:

$dsn = 'mysql://root:123456@localhost/test?charset=utf8&dbcollat=utf8_general_ci';$this->load->database($dsn);

The DSN string is required for PDO initialization. for how to configure it in CI, refer to the following configuration:

// Current Version 2. x. x $ active_group = 'default'; $ active_record = TRUE; $ db ['default'] ['hostname'] = 'MySQL: host = localhost; dbname = test '; $ db ['default'] ['username'] = 'root'; $ db ['default'] ['password'] = '2016 '; $ db ['default'] ['database'] = 'test'; $ db ['default'] ['dbdriver '] = 'pdo '; $ db ['default'] ['dbprefix'] = ''; $ db ['default'] ['pconnect '] = FALSE; $ db ['default'] ['DB _ debug'] = TRUE; $ db ['default'] ['cache _ on'] = FALSE; $ db ['default'] ['cachedir'] = ''; $ db ['default'] ['char _ set'] = 'utf8 '; $ db ['default'] ['dbcollat'] = 'utf8 _ general_ci '; $ db ['default'] ['swap _ pre'] = ''; $ db ['default'] ['autoinit '] = TRUE; $ db ['default'] ['stricton'] = FALSE;

How to connect to multiple databases?

$ This-> load-> database () will assign the database object to the db attribute of CI_Controller. if the database already exists, the database will not be reconnected. That is to say, when $ this-> load-> database () is executed again after $ this-> load-> database ('test'), the second load will not be executed.

However, the second parameter of load allows return. Therefore, you can return and assign a value to the variable to connect to different databases.

$DB1 = $this->load->database('default', TRUE);$DB2 = $this->load->database('test', TRUE);

However, this method takes the initiative to load when needed, which is not easy to use. we can implement it in the MY_Model constructor and assign the returned $ DB1 value to an attribute of CI_Controller, assign a value to or clone the attribute to $ this-> db, for example:

public function __construct($group_name = ''){  parent::__construct();  if($group_name == '') {    $db_conn_name = 'db';  } else {    $db_conn_name = 'db_'.$group_name;  }  $CI = & get_instance();  if(isset($CI->{$db_conn_name}) && is_object($CI->{$db_conn_name})) {    $this->db = $CI->{$db_conn_name};  } else {    $CI->{$db_conn_name} = $this->db = $this->load->database($group_name, TRUE);  }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.