ThinkPHP3.2.3 Database Settings new features
The previous article, we summed up the next ThinkPHP3.2 in the new changes, this article we have to look at the database of what the new features, very detailed, the need for the small partner reference.
The ThinkPHP3.2.3 version of the database driver is fully rewritten with PDO, which is more flexible and powerful than the previous version, and we understand how to use it.
First, the database configuration information for 3.2.3 is adjusted, and 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 ' + ', '//user name
' Db_pwd ' and ' = ',//password
' Db_port ' = ', '//Port
' Db_prefix ' = ', '//database table prefix
' Db_params ' = = Array (),//database connection parameters
' Db_debug ' + TRUE,//Database debug mode can log SQL logs after opening
' Db_lite ' = false,//use database LITE mode
' Db_fields_cache ' = = true,//enable field caching
' Db_charset ' = ' utf8 ',//Database encoding defaults to UTF8
' Db_deploy_type ' + 0,//Database deployment method: 0 Centralized (single server), 1 distributed (Master-slave server)
' Db_rw_separate ' = false,//database read/write is separate master-slave valid
' Db_master_num ' + 1,//read/write separation of the number of servers
' Db_slave_no ' + ',//Specify the number from the server
As opposed to version 3.2.2, the following setting parameters were canceled:
The code is as follows:
' Db_fieldtype_check '//3.2.3 force field type detection
' Db_sql_build_cache '//3.2.3 canceled SQL Create cache
' Db_sql_build_queue '//3.2.3 canceled SQL Create cache
' Db_sql_build_length '//3.2.3 canceled SQL Create cache
' Db_sql_log '//replaced by the new Db_debug parameter
' Db_bind_param '//new version with PDO automatic parameter binding without setting
The new database setup parameters include:
The code is as follows:
' Db_debug '//used to turn on database debug mode and log SQL log when turned on
' Db_lite '//whether using database LITE mode connection can only use native SQL query
The debug mode of the 3.2.2 version database and the debug mode of the project (defined by the App_debug constant) are bound, 3.2. Version 3 the debug mode of the database is set independently (set by the Db_debug parameter).
The Db_type parameter is set for the database type, and currently supported drivers include Mysql/sqlite/oracle/pgsql/sqlsrv/firebird (additional database types require additional drivers), set as follows:
' Db_type ' + ' mysql ',//no longer supports set as 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 with IP address
' Db_name ' = ', '//database name
' Db_user ' + ', '//user name
' Db_pwd ' and ' = ',//password
' Db_port ' = ', '//port left blank then default port
' Db_charset ' = ', '//Database code
The above setting parameters are automatically converted to PDO's connection parameters when the PDO is instantiated.
DB_DSN parameters generally do not need to be set, the system's database driver will be the default settings, if you need to adjust, follow the PDO of the relevant database connection DSN settings to set.
The db_params is used to set the connection parameters of the database, passing in the fourth parameter of the 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 ',//user name
' Db_pwd ' = ' 1234 ',//password
' Db_port ' = ' 3306 ',//Port
' Db_prefix ' = ' think_ ',//database table prefix
' Db_charset ' = ' utf8 ',//Database encoding
' Db_debug ' + TRUE,//Database debug mode can log SQL logs after opening
If you set a separate database connection information Connection property in the model class, you can use the following array or string method:
The code is as follows:
Setting database connection information separately in the model
namespace Home\model;
Use Think\model;
Class Usermodel extends model{
Defined by array method
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 settings parameter that is set in the model takes a globally configured lowercase name.
or defined as a string, in the form:
Database type://user name: password @ Database address: Database port/Database name # Character Set
For example:
The code is as follows:
Setting database connection information separately in the model
namespace Home\model;
Use Think\model;
Class Usermodel extends model{
Define using a String method
protected $connection = ' Mysql://root:1234@192.168.1.10:3306/thinkphp#utf8 ';
}
You can also configure file settings, 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 inside the model:
The code is as follows:
Setting database connection information separately in the model
namespace Home\model;
Use Think\model;
Class Usermodel extends model{
Invoking database configuration in a configuration file 1
protected $connection = ' db_config1 ';
Or
protected $connection = ' db_config2 ';
}
In addition to specifying the database connection information when the model is defined, we can also specify the database connection information at the time of instantiation, and if you are using the M method instantiation model, you can also support passing in different database connection information, for example:
The code is as follows:
$User = M (' User ', ' other_ ', ' Mysql://root:1234@192.168.1.10/demo#utf8 ');
Represents the instantiation of the user model, which is connected to the Other_user table of the demo database, and the connection information is configured with the third parameter.
If we have already configured the DB_CONFIG2 in the project configuration file, we can also use:
$User = M (' User ', ' other_ ', ' db_config2 ');
The above is the whole content of this article, I hope you can enjoy.
http://www.bkjia.com/PHPjc/963996.html www.bkjia.com true http://www.bkjia.com/PHPjc/963996.html techarticle ThinkPHP3.2.3 Database Settings new features in the previous article, we summed up the new changes in the next ThinkPHP3.2, we have to look at the details of the database this piece of what the new features, very ...