Home  >  Article  >  Backend Development  >  ThinkPHP3.2.3 database settings new features, thinkphp3.2.3_PHP tutorial

ThinkPHP3.2.3 database settings new features, thinkphp3.2.3_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:05:30890browse

ThinkPHP3.2.3 database settings new features, thinkphp3.2.3

ThinkPHP3.2.3 version database driver is completely rewritten using PDO, and 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:

Copy code The code is as follows:
/* Database settings */
'DB_TYPE' => '', // Database type
'DB_HOST'                                                                                                                                              => 'DB_NAME'                                                                                              => 'DB_USER'                                                                                                       => 'Db_pwd' = & gt; '', // password
'DB_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' = & gt; 'utf8', // database coding defaults UTF8
'DB_DEPLOY_TYPE' => 0, // Database deployment method: 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:

Copy code

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

Copy code

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:

Copy code The code is as follows:
'DB_HOST' => '', // Server address uses IP address
'DB_NAME'                                                                                              => 'DB_USER'                                                                                                       => 'Db_pwd' = & gt; '', // password
'DB_PORT'                                                                     => '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:

Copy code

The code is as follows: 'DB_TYPE'                                                                                         => 'DB_HOST' => '192.168.1.10', // Server address 'DB_NAME' => 'thinkphp', // Database name 'DB_USER'                                                                                                                             => 'Db_pwd' = & gt; '1234', // password
'DB_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:



Copy code

The code is as follows:

//Set database connection information separately in the model

namespace HomeModel; use ThinkModel; class UserModel extends Model{ // Define in 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:

Copy code

The code is as follows:

//Set database connection information separately in the model

namespace HomeModel; use ThinkModel; class UserModel extends Model{ // Define using string Protected $connection = 'mysql://root:1234@192.168.1.10:3306/thinkphp#utf8';
}


can also be set through configuration files, for example:

Copy code 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:

Copy code 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:

Copy code 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/963838.htmlTechArticleThinkPHP3.2.3 database settings new features, thinkphp3.2.3 ThinkPHP3.2.3 version database driver is completely rewritten using PDO, configuration And the use of the above is also more flexible and powerful than the previous version, let’s...
Statement:
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