


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~

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.


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

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools
