YII 接口内连接其他数据库方法

 // 连接的其他数据库
        $connection = new \yii\db\Connection([
            'dsn' => 'mysql:host=10.10.2.171;dbname=databasename',
            'username' => 'admin',
            'password' => '1234567',
            'charset' => 'utf8',
        ]);
        $connection->open();

        // 筛选条件
        $where = 'where 1=1' ;
        $where = $user_id ? $where.' and UserID = '.$user_id :$where;
        $where = $ObjectType ? $where.' and  ObjectType = '.$ObjectType :$where;
        $where = $type ? $where.' and SubType = '.$type : $where;
        if($start && $end){
            $where = $where." and   '".$start." 00:00:00 '"." <= CreateTime and  CreateTime <= '".$end." 23:59:59 '";
        }

        if($ObjectID){
            $infos_quary = $connection->createCommand("SELECT CameraID FROM CameraInfo where CameraName like '%".$ObjectID."%'");
            $infos = $infos_quary->queryAll();

            $ids = [];
            foreach ($infos as $key=>$value){
                $ids[] =$value['CameraID'];
            }
            if($ids){

                $where = $where.' and ObjectID in ('.implode(",",$ids).')';
            }
        }

        // 总数:分页使用
        $commandall = $connection->createCommand('SELECT * FROM OperationLog '.$where);
        $countriesall = $commandall->queryAll();

        // 拼接查询语句
        $command = $connection->createCommand('SELECT * FROM OperationLog '.$where.' ORDER BY  OperationID DESC limit '.$page.','.$page_size);
        $countries = $command->queryAll();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值