"Turn" CodeIgniter configuration of database

Source: Internet
Author: User
Tags dsn pconnect codeigniter

The CodeIgniter database configuration file is located in application/config/database.php, which defines a two-dimensional array of $db, with the following reference file:

123456789101112131415161718 $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 a one-dimensional key name in $db, which means that the default use of the database configuration, that is $this->load->database () does not pass in parameters, will be used by default $db[$active _group] To connect to the database.

$active _record whether to turn on AR mode, you can use the method in the AR class when turned on, which can be passed in by the third parameter of $this->load->database ().

Places to be aware of $db arrays

1, port By default only lists the host, account number, password, etc., the port number is not configured, if you need to specifically specify the port number you need to configure the value.

2, Pconnect long connection problem, the default value of TRUE indicates that a long connection is used by default. The use of long connections requires special care, the database may be a large number of sleep processes resulting in more request execution is unsuccessful, it is not recommended to open a long connection.

3, Db_debug is true when the SQL execution error will be printed directly on the error page, the development environment can be opened, production environment needs to be closed.

4, Autoinit whether the database is automatically initialized, when True, $this->load->database () will connect to the database, otherwise the database is connected at query time. CI classes do a singleton, so don't worry about multiple links.

5, Stricton when the value is true, the initialization will execute such a statement, the irregular data, such as the character exceeds the length, the self-increment primary key incoming "" and so will be thrown directly wrong.

1 SET SESSION sql_mode="STRICT_ALL_TABLES"
How do I connect to a database?

Can be called by the database method in loader, i.e. $this->load->database (); The function is defined as follows:

123456789 /** * Database Loader * * @param    string  数据库连接值,数组或DSN字符串传递。 * @param    bool    是否返回数据库对象,否则将数据库对象赋值给控制器的db属性 * @param    bool    是否使用AR,这里的设置会覆盖database.php中设置 * @return   object */function database($params = ‘‘, $return = FALSE, $active_record = NULL){}

There are 3 values for $params, namely:

1, string, passed in the $db array one-dimensional key name, such as default test, and so on, is empty the value defined by the defaults $active_group

2, arrays, you can directly pass in a one-dimensional array similar to $DB, such as:

1234567891011 $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 as:

12 $dsn= ‘mysql://root:[email protected]/test?charset=utf8&dbcollat=utf8_general_ci‘;$this->load->database($dsn);

PDO initialization requires the use of a DSN string, then in the CI How to configure it, you can refer to the following configuration:

12345678910111213141516171819 //当前版本2.x.x$active_group = ‘default‘;$active_record = TRUE;$db[‘default‘][‘hostname‘] = ‘mysql:host=localhost;dbname=test‘;$db[‘default‘][‘username‘] = ‘root‘;$db[‘default‘][‘password‘] = ‘123456‘;$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 do I connect multiple databases?
$this->load->database () assigns the database object to the DB attribute of Ci_controller, and does not reconnect if the DB is already present. That is, the second load will not execute after $this->load->database () is executed again $this->load->database (' Test ').

But the second parameter of load allows return, so it can be returned and assigned to a variable to achieve the purpose of a different library.

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

But this method needs to use the time to take the initiative to load, the use is not very convenient, we can be implemented in the My_model constructor, the returned $DB1 is re-assigned to a property of Ci_controller, and the property is assigned or clone to $this->db, For example:

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

--eof--

"Turn" CodeIgniter configuration of database

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.