Home  >  Article  >  PHP Framework  >  How to switch database in thinkphp323 model

How to switch database in thinkphp323 model

王林
王林Original
2019-09-12 11:39:162853browse

How to switch database in thinkphp323 model

When processing programs, we often encounter data that needs to be processed in different databases, so we need to connect two different databases. So how to switch databases in the model?

1. First create a data model.

How to switch database in thinkphp323 model

#2. Create a new method in the model to query the data of the table in the data and return it.

How to switch database in thinkphp323 model

3. Also create a method in the controller to print out the data returned by calling the method in the model.

How to switch database in thinkphp323 model

4. There are two different ways to connect two different databases sww3 and book

in the module configuration file.

How to switch database in thinkphp323 model

5. Use table member to connect data DB_CONFIG1 in the model, and use DB_CONFIG2 to connect data 2 to link data table blog_category

How to switch database in thinkphp323 model

6. Use the browser to control and comment out the code for connecting data 2. First, only connect to database 1 to view the running results.

How to switch database in thinkphp323 model

How to switch database in thinkphp323 model

7. Use browser control and comment out the code connecting data 1 first. Only connect to database 2 to view the running results. It can be run twice and it can display the matching results after running, proving that the database switch is successful.

How to switch database in thinkphp323 model

How to switch database in thinkphp323 model

If you want to learn more related content, please visit the php Chinese website.

Recommended tutorial: thinkphp tutorial

The above is the detailed content of How to switch database in thinkphp323 model. For more information, please follow other related articles on the PHP Chinese website!

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