Home >Backend Development >PHP Tutorial >New features of ThinkPHP3.2.3 database settings_PHP tutorial
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:
Compared with version 3.2.2, the following setting parameters have been cancelled:
The code is as follows:
New database setting parameters include:
The code is as follows:
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:
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:
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:
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:
can also be set through configuration files, for example:
The code is as follows:
Then define it in the model:
The code is as follows:
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:
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.