Home > Backend Development > PHP Tutorial > CodeIgniter configuration database.php usage example analysis, codeigniterdatabase_PHP tutorial

CodeIgniter configuration database.php usage example analysis, codeigniterdatabase_PHP tutorial

WBOY
Release: 2016-07-12 09:00:05
Original
867 people have browsed it

CodeIgniter configuration database.php usage example analysis, codeigniterdatabase

This article provides an example analysis of CodeIgniter configuration database.php usage. Share it with everyone for your reference, the details are as follows:

CodeIgniter’s database configuration file 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;
Copy after login

Configuration instructions

$active_group is a one-dimensional key name in $db, indicating the database configuration used by default, that is, when $this->load->database() does not pass in parameters, it will be used by default $db[$active_group] to connect to the database.

$active_record Whether to turn on AR mode. After turning it on, you can use the methods in the AR class. This value can be passed in through the third parameter of $this->load->database() .

Things to note about the $db array

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

2. The problem of pconnect long connection. The default value is TRUE, which means long connection is used by default. You need to be particularly careful when using long connections. A large number of sleep processes may occur in the database, causing more requests to fail to execute. It is not recommended to enable long connections here.

3. When db_debug is TRUE, SQL execution errors will be printed directly on the error page. The development environment can be opened, but the production environment needs to be closed.

4. Whether autoinit automatically initializes the database. If it is true, $this->load->database() will connect to the database, otherwise it will connect to the database during query. All CI classes are singletons, so you don’t have to worry about multiple links.

5. stricton When the value is TRUE, such a statement will be executed during initialization. For non-standard data, such as characters exceeding the length, auto-incremented primary key passed in '', etc., an error will be thrown directly.
Copy code The code is as follows: SET SESSION sql_mode="STRICT_ALL_TABLES"
How to connect to the database?

Can be called through the database method in Loader, that is, $this->load->database(); The function is defined as follows:

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

There are three situations for the value of $params, namely:

1. String, pass in the one-dimensional key name of the $db array, such as default test, etc. If it is empty, the value defined by $active_group will be defaulted

2. Array, you can directly pass in a one-dimensional array similar to $db, 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',
));

Copy after login

3. DSN string, such as:

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

Copy after login

The initialization of PDO requires the use of DSN strings, so how to configure it in CI, you can refer to the following configuration:

//当前版本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;

Copy after login

How to connect multiple databases?

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

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

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

Copy after login

But this method needs to be actively loaded when used. It is not convenient to use. We can implement it in the constructor of MY_Model, reassign the returned $DB1 to an attribute of CI_Controller, and assign or clone the attribute Give $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);
  }
}

Copy after login

Readers who are interested in more CodeIgniter related content can check out the special topics on this site: "codeigniter introductory tutorial" and "CI (CodeIgniter) framework advanced tutorial"

I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.

Articles you may be interested in:

  • CodeIgniter configuration SESSION usage example analysis
  • CodeIgniter configuration routes.php usage example analysis
  • CodeIgniter configuration config.php usage example analysis
  • CI (Codeigniter) Setting enhanced configuration class example
  • Using Smarty3 basic configuration in CodeIgniter
  • Configuring codeigniter framework method under Nginx
  • CI (CodeIgniter) framework configuration
  • Detailed introduction to the basic configuration of CodeIgniter
  • Analysis of CodeIgniter custom configuration file
  • CodeIgniter configuration autoload.php automatic loading usage analysis

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1094763.htmlTechArticleCodeIgniter configuration database.php usage example analysis, codeigniterdatabase This article provides an example analysis of CodeIgniter configuration database.php usage. Share it with everyone for your reference, the details are as follows:...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template