The database.php usage of codeigniter configuration is analyzed in this paper. Share to everyone for your reference, specific as follows:
The CodeIgniter database configuration file is located in application/config/database.php, which defines a two-dimensional array of $db, with reference to the following 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 in $db, which indicates that the database configuration used by default, that is, $this->load->database () will be used by default when the parameter is not passed in $db[$active _group] To connect to the database.
$active _record to turn on AR mode, the method in the AR class can be used after opening, and the value can be passed in by the third parameter of $this->load->database ().
$db an array of places to note
1, port by default only listed the host, account number, password, etc., did not configure the port number, if you need to specify a special port number you need to configure the value.
2, Pconnect long connection problem, the default value of TRUE indicates the default use of long connections. The use of long connections requires special care, the database may appear a large number of sleep processes and lead to more requests to perform unsuccessfully, it is not recommended to open a long connection.
3, Db_debug is true when the SQL execution error will be directly printed on the error page, the development environment can be opened, the production environment needs to be closed.
4, Autoinit whether the database is automatically initialized, when True then $this->load->database () will connect to the database, otherwise connect the database when querying. CI classes are all done in a single case, so don't worry about multiple links.
5, Stricton When this value is true, the initialization of the execution of such a statement, the irregular data, such as the character over the length, the introduction of the "" and so on, will be directly thrown wrong.
Copy Code code as follows:
SET session sql_mode= "Strict_all_tables"
How do I connect to a database?
Can be called through the database method in loader, namely $this->load->database (); The function is defined as follows:
/**
* db Loader
*
* @param String database connection value, array, or DSN string delivery.
* @param bool Returns the database object, otherwise assigns the database object to the controller's DB property
* @param whether the bool uses AR, The settings here will overwrite the database.php set
* @return object
/function database ($params = ', $return = FALSE, $ Active_record = NULL) {}
There are 3 different values for the $params, respectively:
1, string, incoming $db array one-dimensional key name, such as default test, NULL then default $active_group defined Value
2, the array, you can pass directly into a similar $db of one-dimensional array, such as:
$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:
$dsn = ' mysql://root:123456@localhost/test?charset=utf8&dbcollat=utf8_general_ci ';
$this->load->database ($DSN);
PDO initialization requires the use of DSN strings, then how to configure 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 '] = ' 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 Ci_controller DB property and does not reconnect if DB already exists. That is, the second load does not execute when the $this->load->database (' test ') is executed again after $this->load->database ().
But the second parameter of load allows return, so it can be returned and assigned to variables to achieve the purpose of a different library.
$DB 1 = $this->load->database (' Default ', TRUE);
$DB 2 = $this->load->database (' Test ', TRUE);
But this way need to use when the initiative to load, not very convenient, we can be implemented in the My_model constructor, the return of the $DB1 to the Ci_controller of a property, and assign the property 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 readers interested in CodeIgniter-related content can view the site topics: "CodeIgniter Introductory Course" and "CI (CodeIgniter) Framework Advanced Course"
I hope this article will help you with the PHP program design based on CodeIgniter framework.