MySql数据库导入新的表里面 php操作

代码如下 

 

 

 

<?php
namespace app\index\Controller;
use think\Db;
use think\Controller;
class Dbsaisiyi extends Controller {

    //方法:index
    public function index() {
        // 定义自己用的函数

        echo "开始数据库操作.....".date('H-i:s');
        
        $new=$this->new();
        $old = $this->old();
        $oldtable = 'article';
        $newtable = 'article';

        //article      
        //查询需要复制的表 然后循环
        $olddata = $old->name($oldtable)->where('sid',36)->select();
        $QQ496631085 = 0;
        foreach ($olddata as $key => $val) {

            $iscunzai =  $new->name($newtable)->where('title',$val['title'])->find();
            if($iscunzai){
                echo("<br>最新的id:".$iscunzai['id']);echo("=>".$val['aid']);
            }else{
                $newdata['create_time'] = $val['t'];
                $newdata['update_time'] = time();

                $newdata['sort'] = $val['o'];
                $newdata['status'] = 1;

                $newdata['cate_id'] = 20;
                $newdata['title'] = $val['title'];
                $newdata['author'] = '';
                $newdata['source'] = '';//来源
                $newdata['content'] = $val['content'];//
                $newdata['summary'] = $val['description'];//摘要
                $newdata['image'] = $val['thumbnail'];//
                // $newdata['images'] = '';//
                // $newdata['download'] = '';//
                // $newdata['tags'] = $val[''];//
                
                $newdata['hits'] = $val['n'];//
                $newdata['keywords'] = $val['keywords'];//
                $newdata['description'] = $val['description'];//描述
                // $newdata['template'] = $val[''];//
                $newdata['url'] = $val['linkurl'];//
                $res = $new->name($newtable)->insert($newdata);
                if($res){
                    $QQ496631085++;
                }
            }

        }
        // dump($olddata);
        exit("一共插入{$QQ496631085}次");


    }
    

    // 连接新的数据库
    public function new(){
        $new =     Db::connect([
          // 数据库类型
          'type'    => 'mysql',
          // 数据库连接DSN配置
          'dsn'     => '',
          // 服务器地址
          'hostname'  => '47.110.61.93',
          // 数据库名
          'database'  => 'cssc',
          // 数据库用户名
          'username'  => 'cssc',
          // 数据库密码
          'password'  => 'dGwy4mENPP34Dtxy',
          // 数据库连接端口
          'hostport'  => '3306',
          // 数据库连接参数
          'params'   => [],
          // 数据库编码默认采用utf8
          'charset'   => 'utf8',
          // 数据库表前缀
          'prefix'   => 'tp_',
        ]);
        return $new;
    }
    //连接久的数据库  
    public function old(){
            $old = Db::connect([
          // 数据库类型
          'type'    => 'mysql',
          // 数据库连接DSN配置
          'dsn'     => '',
          // 服务器地址
          'hostname'  => '127.0.0.1',
          // 数据库名
          'database'  => 'oldsaisiyi',
          // 数据库用户名
          'username'  => 'root',
          // 数据库密码
          'password'  => 'root',
          // 数据库连接端口
          'hostport'  => '3306',
          // 数据库连接参数
          'params'   => [],
          // 数据库编码默认采用utf8
          'charset'   => 'utf8',
          // 数据库表前缀
          'prefix'   => 'qw_',
        ]);
        return $old;
    }

}
 

1679
主攻后端PHP
4660
二次开发Code
1679
长连接Swoole
4640
高并发网站Redis