Table of Contents
CodeIgniter configuration database.php usage example analysis, codeigniterdatabase
Articles you may be interested in:
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

Jul 12, 2016 am 09:00 AM
codeigniter database Configuration

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The perfect combination of PyCharm and PyTorch: detailed installation and configuration steps The perfect combination of PyCharm and PyTorch: detailed installation and configuration steps Feb 21, 2024 pm 12:00 PM

PyCharm is a powerful integrated development environment (IDE), and PyTorch is a popular open source framework in the field of deep learning. In the field of machine learning and deep learning, using PyCharm and PyTorch for development can greatly improve development efficiency and code quality. This article will introduce in detail how to install and configure PyTorch in PyCharm, and attach specific code examples to help readers better utilize the powerful functions of these two. Step 1: Install PyCharm and Python

How to set up Git configuration in PyCharm How to set up Git configuration in PyCharm Feb 20, 2024 am 09:47 AM

Title: How to correctly configure Git in PyCharm In modern software development, the version control system is a very important tool, and Git, as one of the popular version control systems, provides developers with powerful functions and flexible operations. As a powerful Python integrated development environment, PyCharm comes with support for Git, allowing developers to manage code versions more conveniently. This article will introduce how to correctly configure Git in PyCharm to facilitate better development during the development process.

The working principle and configuration method of GDM in Linux system The working principle and configuration method of GDM in Linux system Mar 01, 2024 pm 06:36 PM

Title: The working principle and configuration method of GDM in Linux systems In Linux operating systems, GDM (GNOMEDisplayManager) is a common display manager used to control graphical user interface (GUI) login and user session management. This article will introduce the working principle and configuration method of GDM, as well as provide specific code examples. 1. Working principle of GDM GDM is the display manager in the GNOME desktop environment. It is responsible for starting the X server and providing the login interface. The user enters

Leak reveals key specs of Intel Arrow Lake-U, -H, -HX and -S Leak reveals key specs of Intel Arrow Lake-U, -H, -HX and -S Jun 15, 2024 pm 09:49 PM

IntelArrowLakeisexpectedtobebasedonthesameprocessorarchitectureasLunarLake,meaningthatIntel'sbrandnewLionCoveperformancecoreswillbecombinedwiththeeconomicalSkymontefficiencycores.WhileLunarLakeisonlyavailableasava

Understand Linux Bashrc: functions, configuration and usage Understand Linux Bashrc: functions, configuration and usage Mar 20, 2024 pm 03:30 PM

Understanding Linux Bashrc: Function, Configuration and Usage In Linux systems, Bashrc (BourneAgainShellruncommands) is a very important configuration file, which contains various commands and settings that are automatically run when the system starts. The Bashrc file is usually located in the user's home directory and is a hidden file. Its function is to customize the Bashshell environment for the user. 1. Bashrc function setting environment

How to configure workgroup in win11 system How to configure workgroup in win11 system Feb 22, 2024 pm 09:50 PM

How to configure a workgroup in Win11 A workgroup is a way to connect multiple computers in a local area network, which allows files, printers, and other resources to be shared between computers. In Win11 system, configuring a workgroup is very simple, just follow the steps below. Step 1: Open the "Settings" application. First, click the "Start" button of the Win11 system, and then select the "Settings" application in the pop-up menu. You can also use the shortcut "Win+I" to open "Settings". Step 2: Select "System" In the Settings app, you will see multiple options. Please click the "System" option to enter the system settings page. Step 3: Select "About" In the "System" settings page, you will see multiple sub-options. Please click

Simple and easy-to-understand PyCharm configuration Git tutorial Simple and easy-to-understand PyCharm configuration Git tutorial Feb 20, 2024 am 08:28 AM

PyCharm is a commonly used integrated development environment (IDE). In daily development, using Git to manage code is essential. This article will introduce how to configure Git in PyCharm and use Git for code management, with specific code examples. Step 1: Install Git First, make sure Git is installed on your computer. If it is not installed, you can go to [Git official website](https://git-scm.com/) to download and install the latest version of Git

How to configure and install FTPS in Linux system How to configure and install FTPS in Linux system Mar 20, 2024 pm 02:03 PM

Title: How to configure and install FTPS in Linux system, specific code examples are required. In Linux system, FTPS is a secure file transfer protocol. Compared with FTP, FTPS encrypts the transmitted data through TLS/SSL protocol, which improves Security of data transmission. In this article, we will introduce how to configure and install FTPS in a Linux system and provide specific code examples. Step 1: Install vsftpd Open the terminal and enter the following command to install vsftpd: sudo

See all articles