分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

thinkphp 连接两个库

发布时间:2023-09-06 02:12责任编辑:郭大石关键词:thinkphp

新建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:root@127.0.0.1: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_‘,

thinkphp 连接两个库

原文地址:https://www.cnblogs.com/haima/p/9556154.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved