国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home PHP Framework ThinkPHP Can thinkphp configure a new database separately?

Can thinkphp configure a new database separately?

Dec 13, 2022 am 09:24 AM
thinkphp

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:

//數(shù)據(jù)庫配置信息'DB_TYPE' => 'mysql', // 數(shù)據(jù)庫類型
'DB_HOST' => 'localhost', // 服務(wù)器地址
'DB_NAME' => 'thinkphp', // 數(shù)據(jù)庫名
'DB_USER' => 'root', // 用戶名
'DB_PWD' => '123456', // 密碼
'DB_PORT' => 3306, // 端口
'DB_PREFIX' => 'think_', // 數(shù)據(jù)庫表前綴
'DB_CHARSET'=> 'utf8', // 字符集

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'

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', // 數(shù)據(jù)庫類型
'DB_USER' => 'root', // 用戶名
'DB_PWD' => '', // 密碼
'DB_PREFIX' => 'think_', // 數(shù)據(jù)庫表前綴
'DB_DSN' => 'mysql:host=localhost;dbname=thinkphp;charset=UTF-8'

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:

//在模型里單獨(dú)設(shè)置數(shù)據(jù)庫連接信息
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',
);
}

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

//在模型里單獨(dú)設(shè)置數(shù)據(jù)庫連接信息
namespace Home\ Model;
use Think\ Model;
class UserModel extends Model{
//或者使用DSN定義
protected $connection = 'mysql://root:1234@localhost:3306/thinkphp#utf8';
}

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

//數(shù)據(jù)庫配置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',
),
//數(shù)據(jù)庫配置2
'DB_CONFIG2' => 'mysql://root:1234@localhost:3306/thinkphp#utf8';

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

//在模型里單獨(dú)設(shè)置數(shù)據(jù)庫連接信息
namespace Home\ Model;
use Think\ Model;
class UserModel extends Model{
//調(diào)用配置文件中的數(shù)據(jù)庫配置1
protected $connection = 'DB_CONFIG1';
}
//在模型里單獨(dú)設(shè)置數(shù)據(jù)庫連接信息
namespace Home\ Model;
use Think\ Model;
class InfoModel extends Model{
//調(diào)用配置文件中的數(shù)據(jù)庫配置1
protected $connection = 'DB_CONFIG2';
}

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');

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');

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!

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

Development suggestions: How to use the ThinkPHP framework for API development Development suggestions: How to use the ThinkPHP framework for API development Nov 22, 2023 pm 05:18 PM

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

See all articles