Thinkphp 操作多個數據庫


config 配置文件添加詞句

<?php
return array(
    // 示例 mysql://root:88888888@192.168.0.152:3306/jshs
    'DB2' => 'mysql://username:password@ip:port/datebase',

);

Controll 代碼如下

    /**
     * # +========================================================================
     * # | - @name        連接多個數據庫
     * # | - @author      cq <just_leaf@foxmail.com> 
     * # | - @copyright   zmtek 2018-11-09
     * # +------------------------------------------------------------------------
     * # | - 1.192.168.0.133 是項目主數據庫
     * # | - 2.192.168.0.152 是配置之后的數據庫
     * # | - 1.M('表格','前綴','配置名稱')
     * # +========================================================================
     */
    public function Db2() {
        
        # 數據庫(192.168.0.133)
        $user = M('user') -> order('id desc') -> find();
        var_dump($user);

        # 數據庫(192.168.0.152)
        $user = M('user','js_','DB2') -> order('id desc') -> find();
        var_dump($user);
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM