Home > PHP Framework > ThinkPHP > body text

Can thinkphp configure a new database separately?

藏色散人
Release: 2022-12-13 09:24:40
Original
1682 people have browsed it

thinkphp can configure a new database separately. The setting method is: 1. Add configuration parameters such as "'DB_HOST' => 'localhost'..." in the application configuration file or module configuration file; 2. Define independent database configuration information in the configuration files of different application states; 3. Specify the database connection information when instantiating, with syntax such as "$User = M('User','other_','mysql://root ...".

Can thinkphp configure a new database separately?

The operating environment of this tutorial: Windows 7 system, ThinkPHP version 5, Dell G3 computer.

thinkphp can be used alone Configure a new database?

Yes.

Thinkphp model - connect to the database to configure the model database connection independently

Connect to the database

ThinkPHP has built-in abstraction The database access layer encapsulates different database operations. We only need to use the public Db class to operate without writing different codes and underlying implementations for different databases. The Db class will automatically call the corresponding database driver for processing. Currently The databases include Mysql, SqlServer, PgSQL, Sqlite, Oracle, Ibase, Mongo, and support for PDO.

If the application needs to use a database, the database connection information must be configured. The database configuration file has multiple definitions Method.

1. Global configuration definition

The commonly used configuration method is to add the following configuration parameters in the application configuration file or module configuration file:

//数据库配置信息'DB_TYPE' => 'mysql', // 数据库类型
'DB_HOST' => 'localhost', // 服务器地址
'DB_NAME' => 'thinkphp', // 数据库名
'DB_USER' => 'root', // 用户名
'DB_PWD' => '123456', // 密码
'DB_PORT' => 3306, // 端口
'DB_PREFIX' => 'think_', // 数据库表前缀
'DB_CHARSET'=> 'utf8', // 字符集
Copy after login

The type of database is given by DB_TYPE parameter settings.

The following are currently supported database settings:

Can thinkphp configure a new database separately?

If DB_TYPE uses the PDO type, the database type is determined by the DB_DSN configuration.

Or use the following configuration

'DB_DSN' => 'mysql://root:123456@localhost:3306/thinkphp#utf8'
Copy after login

Using DB_DSN mode definition can simplify the configuration parameters. The DSN parameter format is:

Database type://username:password@database address:database port /Database name#Character set

The character set setting needs to be valid in version 3.2.1 or above. If the character set is not set, the default is utf8.

If both configuration parameters exist at the same time, DB_DSN Configuration parameters take precedence.

Note: If you want to set up a distributed database, DB_DSN configuration is not supported for the time being.

If you use PDO driver, you must first configure **DB_TYPE** as pdo. Then you need to configure other parameters separately, for example:

//PDO连接方式
'DB_TYPE' => 'pdo', // 数据库类型
'DB_USER' => 'root', // 用户名
'DB_PWD' => '', // 密码
'DB_PREFIX' => 'think_', // 数据库表前缀
'DB_DSN' => 'mysql:host=localhost;dbname=thinkphp;charset=UTF-8'
Copy after login

Note: The DB_DSN configuration format of PDO mode is different, and the settings are different according to different database types. For details, please refer to the PHP manual.

The database connection information defined in the configuration file is generally used by the system by default, because generally the database access configuration of an application is the same. In this method, the system will automatically obtain it when connecting to the database, and there is no need to connect manually.

You can define different database connection information for each module. If the debugging mode is turned on, you can also define independent database configuration information in the configuration files of different application states.

2. Model class definition

If the connection attribute is defined in a model class, the defined database connection information will be used when instantiating the custom model instead of the configuration The default connection information set in the file is usually used for some data tables located in other databases outside the current database connection, for example:

//在模型里单独设置数据库连接信息
namespace Home\ Model;
use Think\ Model;
class UserModel extends Model{
protected $connection = array(
'db_type' => 'mysql',
'db_user' => 'root',
'db_pwd' => '1234',
'db_host' => 'localhost',
'db_port' => '3306',
'db_name' => 'thinkphp',
'db_charset' => 'utf8',
);
}
Copy after login

It can also be defined in DSN mode, for example:

//在模型里单独设置数据库连接信息
namespace Home\ Model;
use Think\ Model;
class UserModel extends Model{
//或者使用DSN定义
protected $connection = 'mysql://root:1234@localhost:3306/thinkphp#utf8';
}
Copy after login

If we have configured additional database connection information in the configuration file, for example:

//数据库配置1
'DB_CONFIG1' => array(
'db_type' => 'mysql',
'db_user' => 'root',
'db_pwd' => '1234',
'db_host' => 'localhost',
'db_port' => '3306',
'db_name' => 'thinkphp',
'db_charset'=> 'utf8',
),
//数据库配置2
'DB_CONFIG2' => 'mysql://root:1234@localhost:3306/thinkphp#utf8';
Copy after login

Then, we can change the attribute definition of the model class to:

//在模型里单独设置数据库连接信息
namespace Home\ Model;
use Think\ Model;
class UserModel extends Model{
//调用配置文件中的数据库配置1
protected $connection = 'DB_CONFIG1';
}
//在模型里单独设置数据库连接信息
namespace Home\ Model;
use Think\ Model;
class InfoModel extends Model{
//调用配置文件中的数据库配置1
protected $connection = 'DB_CONFIG2';
}
Copy after login

3. Instantiation definition

In addition to specifying the database connection information when defining the model, we can also specify the database connection information when instantiating it. For example: if the M method is used to instantiate the model, also It can support passing in different database connection information, for example:

$User = M('User','other_','mysql://root:1234@localhost/demo#utf8');
Copy after login

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');
Copy after login

It should be noted that ThinkPHP's database connection is lazy, so it does not connect to the database when instantiated, but The database will be connected only when there is actual data operation (in addition, when the system instantiates the model for the first time, it will automatically connect to the database to obtain the field information of the data table corresponding to the relevant model class).

Recommended learning: "thinkPHP Video Tutorial"

The above is the detailed content of Can thinkphp configure a new database separately?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template