ThinkPHP connection to the database and master-slave database settings tutorial, thinkphp master-slave. ThinkPHP connection to the database and master-slave database settings tutorial, thinkphp master-slave This article describes in detail ThinkPHP connection to the database and master-slave database settings, in the ThinkPHP project, set ThinkPHP to connect to the database and master-slave database. thinkphp master-slave
This article describes how to set ThinkPHP to connect to the database and master-slave database in detail. it is very useful 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 ', // Database type 'DB _ host' => 'localhost', // HOST 'DB _ name' => 'aoli ', // database name 'DB _ user' => 'root', // database username 'DB _ pwd' => '', // database password 'DB _ prefix' => '', // data table PREFIX 'DB _ charset' => 'utf8 ', // website code 'DB _ port' => '123', // database PORT);?>
2. set the project configuration file
The code for the \ Home \ Conf \ config. php file is as follows:
<? Php $ arr1 = array {'URL _ model' => 2, // pathinfo access mode}; $ arr2 = include '. /config. php '; return array_merge ($ arr1, $ arr2); // array integration?>
The code for the \ Admin \ Conf \ config. php file is as follows:
<? Php $ arr1 = array {'URL _ model' => 1, // get mode in normal access mode}; $ arr2 = include '. /config. php '; return array_merge ($ arr1, $ arr2); // array integration?>
III. master/slave database settings
This setting is suitable for large websites with high concurrency and high load.
You can view the default system constant settings in \ ThinkPHP \ Common \ convention. php.
The config. php file is set as follows:
<? Php return array (// 'config map '=> 'configuration value' // backend 'URL _ mode' => 0, 'DB _ type' => 'mysql ', 'db _ host' => 'localhost, 192.168.1.2 ', // Two database servers 'DB _ port' => '123 ', 'db _ name' => 'thinkphptest', // if the database NAME is the same, you do not need to define multiple, if they are different from those on the server, they correspond to 'DB _ user' => 'root', 'DB _ pwd' => 'password ', // table PREFIX 'DB _ prefix' => 'think _ ', // Configure the master-slave database 'DB _ DEPLOY_TYPE' => 1, // enable the distributed database 'DB _ RW_SEPARATE '=> ture, // read/write splitting. by default, the first server is the write server, and the others only read and write data.);?>
Read database file parameters in an action:
$ Hh = C ('Db _ host'); // C can read the value $ pp = C ('Db _ prefix') in the configuration file '); $ this-> assain ('H', $ hh); $ this-> assain ('P', $ pp); $ this-> display ();
Tpl under this action:
Database server address: {$ h} database table prefix: {$ p}
I hope this method will be helpful for ThinkPHP programming.
Which of the following types of thinkphp master/slave databases can be used?
As far as I know. You can set multiple databases, but it is not set as you do ....
Reading more help documents will help you ., This multi-database setting should be available in the help document ..
How to connect to the database in ThinkPHP [switch]
Before connecting to the database to operate the database in ThinkPHP, we need to create a Model. Before talking about Model and Action, first explain the storage location 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 rule of the ThinkPHP system is like this: the civilized 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 in config. DB_PREFIX + Model class name defined in php. Model class name and file name must be capitalized. "in the Model file, define a class and extend the Model class, the general syntax is as follows: class name Model extends Model {}. now let's define a Model. Because our database table name is cms_article, and the class ArticleModel extends Model {} file is saved as ArticleModel. class. php. No need to write anything. a Model has been defined. Now, let's continue with our knowledge about Action. Action and Model are very similar. The difference is that Action does not directly operate the database, but needs to operate the database through the Model. Now let's define an Action to complete the operation. Class IndexAction extends Action {function index () {$ Article = D ("Article") ;}} saves 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 definitions are normal. In Action, the D method calls Model, and the Article method is the Model class in ArticleModel. class. php we just defined ~ That is to say, while defining the Model, we have completed the connection to the database and the quasi-standby operation to the database table ~
This article describes in detail how to set ThinkPHP to connect to the database and master-slave database...