Database.php Usage Example Analysis of codeigniter configuration, codeigniterdatabase_php tutorial

Source: Internet
Author: User
Tags pconnect codeigniter

Database.php Usage Example Analysis of codeigniter configuration, Codeigniterdatabase


In this paper, the database.php usage of codeigniter configuration is analyzed. Share to everyone for your reference, as follows:

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:

$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.
Copy the Code code as follows: 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:

/** * Database Loader * * @param string  data connection value, array or DSN string passed. * @param  bool  Returns the database object, otherwise assigns the database object to the controller's DB property * @param  bool  uses AR, where the setting overrides the setting in 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:

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

3, DSN string, such as:

$dsn = ' mysql://root:123456@localhost/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:

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 '] = ' 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.

$DB 1 = $this->load->database (' default ', true); $DB 2 = $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:

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

More interested in CodeIgniter related content readers can view this site topic: "CodeIgniter Introductory Tutorial" and "CI (codeigniter) Framework Advanced Tutorial"

It is hoped that this article is helpful to the PHP program design based on CodeIgniter framework.

Articles you may be interested in:

    • Example analysis of session usage of CodeIgniter configuration
    • Example analysis of routes.php usage of codeigniter configuration
    • Example analysis of config.php usage of codeigniter configuration
    • Setting enhanced configuration class instance for CI (CodeIgniter)
    • Using Smarty3 basic configuration in CodeIgniter
    • CodeIgniter Framework method under Nginx configuration
    • CI (codeigniter) Framework Configuration
    • CodeIgniter Basic Configuration Detailed Introduction
    • Parsing the CodeIgniter custom configuration file
    • autoload.php Automatic load usage analysis of CodeIgniter configuration

http://www.bkjia.com/PHPjc/1094763.html www.bkjia.com true http://www.bkjia.com/PHPjc/1094763.html techarticle database.php Usage Example Analysis of CodeIgniter configuration, codeigniterdatabase This paper analyzes the database.php usage of codeigniter configuration. Share to everyone for your reference, as follows: ...

  • 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.