Home >Backend Development >PHP Tutorial >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_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:20:101030browse

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:

<&#63;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',// 数据库端口
 );
&#63;>

2. Set project configuration file

HomeConfconfig.php file code is as follows:

<&#63;php
 $arr1=array{
  'URL_MODEL'=>2, //pathinfo访问模式
 };
 $arr2=include './config.php';
 return array_merge($arr1,$arr2); //数组整合
&#63;>

AdminConfconfig.php file code is as follows:

<&#63;php
 $arr1=array{
  'URL_MODEL'=>1, //普通访问模式 get方式
 };
 $arr2=include './config.php';
 return array_merge($arr1,$arr2); //数组整合
&#63;>

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:

<&#63;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,//读写分离,默认第一台服务器为写入服务器,其它的只读取不写入
  );
&#63;>

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.

Can there be multiple types of thinkphp master-slave database?

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 [Repost]

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~

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/868235.htmlTechArticleThinkPHP connection database and master-slave database setting tutorial, thinkphp master-slave article describes in detail ThinkPHP connection database and How to set up the master-slave database, open in ThinkPHP project...
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