New features of ThinkPHP3.2.3 database settings
In the previous article, we summarized the new changes in ThinkPHP3.2. In this article, we will take a detailed look at the database. What are the new features? It’s very detailed. Friends who need it can refer to it.
ThinkPHP3.2.3 version database driver is completely rewritten using PDO. The configuration and use are more flexible and powerful than the previous version. Let’s learn how to use it.
First of all, the database configuration information of 3.2.3 has been adjusted. The complete database settings include:
The code is as follows:
/* Database settings */
'DB_TYPE' => '', // Database type
'DB_HOST' => '', // Server address
'DB_NAME' => '', // Database name
'DB_USER' => '', // Username
'DB_PWD' => '', // Password
'DB_PORT' => '', // Port
'DB_PREFIX' => '', // Database table prefix
'DB_PARAMS' => array(), // Database connection parameters
'DB_DEBUG' => TRUE, // Database debugging mode can record SQL logs after it is turned on
'DB_LITE' => false, // Use database Lite mode
'DB_FIELDS_CACHE' => true, // Enable field caching
'DB_CHARSET' => 'utf8', // The default database encoding is utf8
'DB_DEPLOY_TYPE' => 0, // Database deployment mode: 0 centralized (single server), 1 distributed (master-slave server)
'DB_RW_SEPARATE' => false, // Whether database reading and writing are separated, master-slave mode is valid
'DB_MASTER_NUM' => 1, // Number of master servers after read and write separation
'DB_SLAVE_NO' => '', // Specify the slave server serial number
Compared with version 3.2.2, the following setting parameters have been cancelled:
The code is as follows:
'DB_FIELDTYPE_CHECK' // 3.2.3 forces field type detection
'DB_SQL_BUILD_CACHE' // 3.2.3 canceled the SQL creation cache
'DB_SQL_BUILD_QUEUE' // 3.2.3 canceled SQL creation cache
'DB_SQL_BUILD_LENGTH' // 3.2.3 canceled the SQL creation cache
'DB_SQL_LOG' // Replaced by the new DB_DEBUG parameter
'DB_BIND_PARAM' // The new version uses PDO automatic parameter binding, no setting required
New database setting parameters include:
The code is as follows:
'DB_DEBUG' //Used to enable database debugging mode. Once enabled, SQL logs can be recorded
'DB_LITE' // Whether to use database Lite mode to connect. After enabling, only native SQL queries can be used
The debugging mode of the database in version 3.2.2 is bound to the debugging mode of the project (defined by the APP_DEBUG constant). Starting from version 3.2.3, the debugging mode of the database is set independently (set by the DB_DEBUG parameter).
The DB_TYPE parameter is the database type setting. Currently supported drivers include mysql/sqlite/oracle/pgsql/sqlsrv/firebird (other database types require additional drivers). The settings are as follows:
'DB_TYPE'=>'mysql', // No longer supports setting to PDO, and no longer distinguishes between mysql and mysqli
Copy code
Database connection information, mainly including the following parameters:
The code is as follows:
'DB_HOST' => '', // Server address uses IP address
'DB_NAME' => '', // Database name
'DB_USER' => '', // Username
'DB_PWD' => '', // Password
'DB_PORT' => '', // Port If left blank, the default port will be used
'DB_CHARSET' => '', // Database encoding
The above setting parameters will be automatically converted into PDO connection parameters and passed in when instantiating PDO.
The DB_DSN parameter generally does not need to be set. The system's database driver will set it by default. If it needs to be adjusted, please follow the DSN settings of the relevant database connection of PDO.
DB_PARAMS is used to set the connection parameters of the database and will pass in the fourth parameter of PDO instantiation.
The following is a typical database global setting:
The code is as follows:
'DB_TYPE' => 'mysql', // Database type
'DB_HOST' => '192.168.1.10', // Server address
'DB_NAME' => 'thinkphp', // Database name
'DB_USER' => 'root', // Username
'DB_PWD' => '1234', // Password
'DB_PORT' => '3306', // Port
'DB_PREFIX' => 'think_', // Database table prefix
'DB_CHARSET' => 'utf8', // Database encoding
'DB_DEBUG' => TRUE, // Database debugging mode can record SQL logs after it is turned on
If you set a separate database connection information connection attribute in the model class, you can use the following array or string method:
The code is as follows:
//Set database connection information separately in the model
namespace HomeModel;
use ThinkModel;
class UserModel extends Model{
// Define using array
protected $connection = array(
'db_type' => 'mysql',
'db_user' => 'root',
'db_pwd' => '1234',
'db_host' => '192.168.1.10',
'db_port' => '3306',
'db_name' => 'thinkphp',
'db_charset' => 'utf8',
);
}
Note: The database connection setting parameters set in the model adopt the lowercase name of the global configuration.
or defined in string format, the format is:
Database type://username:password@database address:database port/database name#character set
For example:
The code is as follows:
//Set database connection information separately in the model
namespace HomeModel;
use ThinkModel;
class UserModel extends Model{
// Define
using string mode protected $connection = 'mysql://root:1234@192.168.1.10:3306/thinkphp#utf8';
}
can also be set through configuration files, for example:
The code is as follows:
//Database configuration 1
'DB_CONFIG1' => array(
'db_type' => 'mysql',
'db_user' => 'root',
'db_pwd' => '1234',
'db_host' => '192.168.1.10',
'db_port' => '3306',
'db_name' => 'thinkphp',
'db_charset'=> 'utf8',
),
//Database configuration 2
'DB_CONFIG2' => 'mysql://root:1234@192.168.1.10:3306/thinkphp#utf8';
Then define it in the model:
The code is as follows:
//Set database connection information separately in the model
namespace HomeModel;
use ThinkModel;
class UserModel extends Model{
//Call the database configuration 1 in the configuration file
protected $connection = 'DB_CONFIG1';
// or
protected $connection = 'DB_CONFIG2';
}
In addition to specifying the database connection information when defining the model, we can also specify the database connection information when instantiating it. If the M method is used to instantiate the model, different database connection information can also be passed in. For example:
The code is as follows:
$User = M('User','other_','mysql://root:1234@192.168.1.10/demo#utf8');
means instantiating the User model, connecting to the other_user table of the demo database, and the connection information used is configured by the third parameter.
If we have configured DB_CONFIG2 in the project configuration file, we can also use:
$User = M('User','other_','DB_CONFIG2');
The above is the entire content of this article, I hope you all like it.

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于索引优化器工作原理的相关内容,其中包括了MySQL Server的组成,MySQL优化器选择索引额原理以及SQL成本分析,最后通过 select 查询总结整个查询过程,下面一起来看一下,希望对大家有帮助。

sybase是基于客户/服务器体系结构的数据库,是一个开放的、高性能的、可编程的数据库,可使用事件驱动的触发器、多线索化等来提高性能。

visual foxpro数据库文件是管理数据库对象的系统文件。在VFP中,用户数据是存放在“.DBF”表文件中;VFP的数据库文件(“.DBC”)中不存放用户数据,它只起将属于某一数据库的 数据库表与视图、连接、存储过程等关联起来的作用。

数据库系统由4个部分构成:1、数据库,是指长期存储在计算机内的,有组织,可共享的数据的集合;2、硬件,是指构成计算机系统的各种物理设备,包括存储所需的外部设备;3、软件,包括操作系统、数据库管理系统及应用程序;4、人员,包括系统分析员和数据库设计人员、应用程序员(负责编写使用数据库的应用程序)、最终用户(利用接口或查询语言访问数据库)、数据库管理员(负责数据库的总体信息控制)。

microsoft sql server是Microsoft公司推出的关系型数据库管理系统,是一个全面的数据库平台,使用集成的商业智能(BI)工具提供了企业级的数据管理,具有使用方便可伸缩性好与相关软件集成程度高等优点。SQL Server数据库引擎为关系型数据和结构化数据提供了更安全可靠的存储功能,使用户可以构建和管理用于业务的高可用和高性能的数据应用程序。

数据库的“完整性”是指数据的正确性和相容性。完整性是指数据库中数据在逻辑上的一致性、正确性、有效性和相容性。完整性对于数据库系统的重要性:1、数据库完整性约束能够防止合法用户使用数据库时向数据库中添加不合语义的数据;2、合理的数据库完整性设计,能够同时兼顾数据库的完整性和系统的效能;3、完善的数据库完整性有助于尽早发现应用软件的错误。

go语言可以写数据库。Go语言和其他语言不同的地方是,Go官方没有提供数据库驱动,而是编写了开发数据库驱动的标准接口,开发者可以根据定义的接口来开发相应的数据库驱动;这样做的好处在于,只要是按照标准接口开发的代码,以后迁移数据库时,不需要做任何修改,极大方便了后期的架构调整。

mysql查询为什么会慢,关于这个问题,在实际开发经常会遇到,而面试中,也是个高频题。遇到这种问题,我们一般也会想到是因为索引。那除开索引之外,还有哪些因素会导致数据库查询变慢呢?


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
