


ThinkPHP connection database and master-slave database setting tutorial, thinkphp master-slave_PHP tutorial
ThinkPHP connection database and master-slave database setting tutorial, thinkphp master-slave
This article describes in detail how to connect ThinkPHP to the database and set up the master-slave database, which is very practical in ThinkPHP project development. The specific implementation method is as follows:
1. Create config.php in the project root directory
The code is as follows:
<?php if(!defined('THINK_PATH')) exit(); return array( 'DB_TYPE' => 'mysql',// 数据库类型 'DB_HOST' => 'localhost',// 主机 'DB_NAME' => 'aoli',// 数据库名称 'DB_USER' => 'root',// 数据库用户名 'DB_PWD' => '',// 数据库密码 'DB_PREFIX' => '',// 数据表前缀 'DB_CHARSET' => 'utf8',// 网站编码 'DB_PORT' => '3306',// 数据库端口 ); ?>
2. Set project configuration file
HomeConfconfig.php file code is as follows:
<?php $arr1=array{ 'URL_MODEL'=>2, //pathinfo访问模式 }; $arr2=include './config.php'; return array_merge($arr1,$arr2); //数组整合 ?>
AdminConfconfig.php file code is as follows:
<?php $arr1=array{ 'URL_MODEL'=>1, //普通访问模式 get方式 }; $arr2=include './config.php'; return array_merge($arr1,$arr2); //数组整合 ?>
3. Master-slave database settings
This setting is more suitable for large websites with high concurrency and high load
Readers can check the default system constant settings in ThinkPHPCommonconvention.php
The config.php file settings are as follows:
<?php return array( //'配置项'=>'配置值' //后台 'URL_MODE'=>0, 'DB_TYPE'=>'mysql', 'DB_HOST'=>'localhost,192.168.1.2',//两台数据库服务器 'DB_PORT'=>'3306', 'DB_NAME'=>'thinkphptest',//如果数据库名相同,不用定义多个,如果不相同与服务器依次对应 'DB_USER'=>'root', 'DB_PWD'=>'password', //表前缀 'DB_PREFIX'=>'think_', //接下来配置主从数据库 'DB_DEPLOY_TYPE'=>1,//开启分布式数据库 'DB_RW_SEPARATE'=>ture,//读写分离,默认第一台服务器为写入服务器,其它的只读取不写入 ); ?>
Read database file parameters in an action:
$hh=C('DB_HOST'); //C可以读取配置文件中的值 $pp=C('DB_PREFIX'); $this->assain('h',$hh); $this->assain('p',$pp); $this->display();
tpl under this action:
数据库服务器地址:{$h} 数据库表前缀:{$p}
I hope the method described in this article will be helpful to everyone’s ThinkPHP programming design.
As far as I know. You can set up multiple databases, but there is no way you can set them up. . . .
Reading the help documentation will help you. , the help document should have this multi-database setting. .
How to connect to the database in ThinkPHP. Before operating the database, we need to create a Model. Before talking about Model and Action, let me first explain the storage locations of Model and Action. Model is saved in the lib/Model folder in the program directory, and Action is saved in the lib/Action folder in the program directory. The default Model rules of the ThinkPHP system are as follows: the civil name of the Model file is similar to "Model class name+Model.class.php, and the default operating database table name of the Model is DB_PREFIX+Model class name we defined in config.php. Model class names and file names need to be capitalized." In the Model file, define a class and extend the Model class. The general writing method is as follows: class class name Model extends Model{} So, now let's define a Model. Since the name of our database table is cms_article, the class ArticleModel extends Model{} file is saved as ArticleModel.class.php. There is no need to write anything, a Model has been defined. So now, let's continue our Action knowledge. Many rules of Action and Model are very similar. The difference is that Action does not directly operate the database, but needs to operate the database through Model. Now we define an Action to complete the operation. class IndexAction extends Action{function index(){$Article = D("Article");}} Save the file as IndexAction.class.php. OK, now let's refresh the homepage. If there are no prompts, congratulations. The database connection Model and Action definition are all normal. The D method in Action is to call Model, and Article is the Model class in ArticleModel.class.php that we just defined~ In other words, while defining Model, we have completed the connection to the database and preparation for database table operations~

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

Use the DataAccessObjects (DAO) library in C++ to connect and operate the database, including establishing database connections, executing SQL queries, inserting new records and updating existing records. The specific steps are: 1. Include necessary library statements; 2. Open the database file; 3. Create a Recordset object to execute SQL queries or manipulate data; 4. Traverse the results or update records according to specific needs.

PHP database connection guide: MySQL: Install the MySQLi extension and create a connection (servername, username, password, dbname). PostgreSQL: Install the PgSQL extension and create a connection (host, dbname, user, password). Oracle: Install the OracleOCI8 extension and create a connection (servername, username, password). Practical case: Obtain MySQL data, PostgreSQL query, OracleOCI8 update record.
