這篇文章主要介紹利用tp5框架實現(xiàn)多數(shù)據(jù)庫查詢的方法,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
有時候一個管理后臺,需要涉及到多個數(shù)據(jù)庫。比如,商城管理、直播管理、消息管理等等,它們都有自己的數(shù)據(jù)庫。這個時候,就需要去連接多個數(shù)據(jù)庫,進行處理了。thinkphp可以支持多個數(shù)據(jù)庫連接。
如何處理呢?
1、進行多個數(shù)據(jù)庫的配置
默認(rèn)會連接database.php中的數(shù)據(jù)庫信息。
<?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 [ // 數(shù)據(jù)庫類型 'type' => 'mysql', // 服務(wù)器地址 'hostname' => '', // 數(shù)據(jù)庫名 'database' => '', // 數(shù)據(jù)庫用戶名 'username' => '', // 數(shù)據(jù)庫密碼 'password' => '', // 數(shù)據(jù)庫連接端口 'hostport' => '3306', // 數(shù)據(jù)庫編碼默認(rèn)采用utf8 'charset' => '', // 數(shù)據(jù)庫表前綴 'prefix' => '' ];
tp5會自動加載database.php
(推薦教程:thinkphp教程)
我們可以在extra文件夾中,再創(chuàng)建幾個其他數(shù)據(jù)庫的配置,比如database_mall,database_live,database_app等。
2、初始化
在model模塊中進行初始化
<?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、使用
$this->db_app->table('order')->select();
這樣就可以查詢其他數(shù)據(jù)庫中的數(shù)據(jù)了。
下面是全的代碼:
<?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(); // 數(shù)據(jù) 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; } }
以上是“利用tp5框架實現(xiàn)多數(shù)據(jù)庫查詢的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
標(biāo)題名稱:利用tp5框架實現(xiàn)多數(shù)據(jù)庫查詢的方法-創(chuàng)新互聯(lián)
當(dāng)前路徑:http://aaarwkj.com/article38/jdepp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設(shè)、移動網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容