thinkphp5 同时连接两个库

新建api/user.php

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/25
 * Time: 15:20
 */
namespace app\api\controller;

use think\Db;

class User
{

    public $db2;

    public function __construct()
    {
        //方法一:表前缀没有用
        // $this->db2 = Db::connect([
        //     // 数据库类型
        //     'type' => 'mysql',
        //     // 数据库连接DSN配置
        //     'dsn' => '',
        //     // 服务器地址
        //     'hostname' => '127.0.0.1',
        //     // 数据库名
        //     'database' => 'o2o',
        //     // 数据库用户名
        //     'username' => 'root',
        //     // 数据库密码
        //     'password' => 'root',
        //     // 数据库连接端口
        //     'hostport' => '3306',
        //     // 数据库连接参数
        //     'params' => [],
        //     // 数据库编码默认采用utf8
        //     'charset' => 'utf8',
        //     // 数据库表前缀
        //     'prefix' => 'o2o_',
        // ]);

    }

    public function index()
    {
        echo 'api/User';
        echo '<br>';

        $res = db('shop_station')->find(1);
        // $res = db('ship_station')->find(49);
        dump($res);
        echo '<br>';
        //方法一:表前缀没有用
        // $ret = $this->db2->table('o2o_shop_station')->find(1);

        //方法二:表前缀没有用
        $db2 = Db::connect('mysql://root:[email protected]:3306/o2o#utf8');
        $ret = $db2->table('o2o_shop_station')->find(1);
        dump($ret);
    }
}

datebase.php里还是正常配置库一


 // 服务器地址
       'hostname'        => '127.0.0.2',
       // 数据库名
       'database'        => 'o2o',
       // 用户名
       'username'        => 'root2',
       // 密码
       'password'        => 'root2',
       // 端口
       'hostport'        => '3306',
    
       // 数据库表前缀
       'prefix'          => 'o2o_',

猜你喜欢

转载自www.cnblogs.com/haima/p/9556154.html
今日推荐