Foreword:
Sometimes a management background needs to involve multiple databases. For example, mall management, live broadcast management, message management, etc., they all have their own databases. At this time, you need to connect to multiple databases and process them. thinkphp can support multiple database connections.
How to deal with it?
1. Configure multiple databases
The database information in database.php will be connected by default.
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- return [ // 数据库类型 'type' => 'mysql', // 服务器地址 'hostname' => '', // 数据库名 'database' => '', // 数据库用户名 'username' => '', // 数据库密码 'password' => '', // 数据库连接端口 'hostport' => '3306', // 数据库编码默认采用utf8 'charset' => '', // 数据库表前缀 'prefix' => '' ];
tp5 will automatically load database.php
(Recommended tutorial: thinkphp tutorial)
We can create a few more in the extra folder Configuration of other databases, such as database_mall, database_live, database_app, etc.
2. Initialization
Initialize in the model module
<?php namespace app\admin\model; use think\Model; use think\Db; class LiveRecharge extends Model { protected $db_app; function __construct() { $this->db_app = Db::connect('database_app'); } }
3. Use
$this->db_app->table('order')->select();
so that you can query data in other databases.
The following is the complete code:
<?php namespace app\admin\model; use think\Model; use think\Db; class LiveRecharge extends Model { protected $db_app; function __construct() { $this->db_app = Db::connect('database_app'); } // 获取分页 public function getList($customer_id = '',$nickname = '',$paytime = '',$pagesize = '') { $pagesize = $pagesize && $pagesize > 0 ? $pagesize : config('default_page_size'); $where = array(); $where['o.type'] = 3; if ($customer_id) { $where['o.uid'] = $customer_id; } if ($nickname) { $where['c.NickName'] = ['like','%'.$nickname.'%']; } if ($paytime) { $where['o.addtime'] = array(['>',$paytime.' 00:00'], ['<',$paytime.' 23:59']); } $result = $this->db_app->table('order') ->alias('o') ->where($where) ->join('customer c','o.uid = c.Id') ->field('o.*,c.NickName as nickname')->paginate($pagesize,false,[ 'query' => [ 'customer_id'=>$customer_id, 'nickname'=>$nickname, 'paytime'=>$paytime ] ]); $page = $result->render(); // 分页 $data = $result->all(); // 数据 foreach ($data as $k=>$v) { $data[$k]['diamond'] = intval($v['money'])*10; } // dump($this->db_app->getLastSql()); $total_diamond = $this->db_app->table('order')->where('type',3)->sum('money*10'); $outData['page'] = $page; $outData['data'] = $data; $outData['total_diamond'] = $total_diamond; return $outData; } }
For more programming-related content, please pay attention to the Programming Introduction column on the php Chinese website!
The above is the detailed content of How to use the tp5 framework to implement multi-database queries. For more information, please follow other related articles on the PHP Chinese website!