CodeIgniter configuration-database. php usage instance analysis-php instance

Source: Internet
Author: User
Tags pconnect codeigniter
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

$ Active_group is the one-dimensional key name in $ db, which indicates the default database configuration, that is, when $ this-> load-> database () does not input parameters, $ db [$ active_group] is used to connect to the database by default.

$ Active_record: whether to enable the AR mode. After the AR mode is enabled, you can use the method in the AR class. this value can be passed in through the third parameter of $ this-> load-> database.

$ Notes for db Arrays

1. port only lists hosts, accounts, and passwords by default. No port number is configured. If you need to specify a port number, you need to configure this value.

2. pconnect persistent connections. If the default value is TRUE, persistent connections are used by default. You need to be careful when using persistent connections. A large number of sleep processes may occur in the database, resulting in the execution of more requests being unsuccessful. We do not recommend enabling persistent connections here.

3. If db_debug is TRUE, an SQL Execution error is printed directly on the error page. The development environment can be opened and the production environment must be closed.

4. Whether autoinit automatically initializes the database. If it is set to true, $ this-> load-> database () will connect to the database. Otherwise, the database will be connected during query. CI classes are used as Singleton instances, so there is no need to worry about multiple links.

5. When the value of stricton is TRUE, such a statement will be executed during initialization and non-standard data will be processed, for example, if the length of a character exceeds the length, or the auto-increment primary key is passed in, an error is thrown.

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.