描述:一个A库 一个B库
需求:将A库中某几张表导入到B库中
前提:两个表字段结构相同的前提下
为什么要分批导入?
因为在数据庞大的情况下 浏览器扛不住
如果A库往B库导入数据 需在B库建立一张表 表名jlb
B代码
Client.php
<?php
header("content-type:text/html;charset=utf-8");
date_default_timezone_set("PRC");
execr();
function execr(){
// -------------------------------------------------------------------
// 日志相关
//$path = __DIR__.'/log/'; // 日志路径 Linux
$path = __DIR__.'\\log\\'; // 日志路径 windows
if(!file_exists($path)){
mkdir($path);
}
$filename = date('Ymd'); // 日志名
// 删除30天前的日志
$delfile = $path.date('Ymd',strtotime('-1 day')).'.log';
if (file_exists($delfile)) {
unlink($delfile);
}
// -------------------------------------------------------------------
// -------------------------------------------------------------------
$serverUrl = array( // 服务端地址
"localhost/a/Server.php",
);
$row_per = 5000; // 每次从服务器读取多少条数据
$key = "qweasdzxc"; // 加密key
$configClient = array( // Client库连接信息
'DB_HOST' => 'localhost',
'DB_NAME' => 'zdyxm',
'DB_USER' => 'root',
'DB_PWD' => '',
'DB_PORT' => '3306',
);
// -------------------------------------------------------------------
//调用Config.php
require_once('./Config.php');
$dbClient = new base($configClient);
//客户端记录表
$bm = 'jlb';
// 循环获取各场数据
foreach ($serverUrl as $url) {
$tableNameArray = array( // 需同步表数组
'a' => [''], //多个库的话需将id,填入数组
'b' => [''],
'c' => [''],
'd' => [''],
'e' => [''],
);
foreach ($tableNameArray as $table => $filed) {
// 循环获取各表数据
do{
//查询表的最大ID
$maxid = $dbClient->querySql("SELECT tableid FROM $bm WHERE url = '$url' AND tablename = '$table'");
if (empty($maxid)) {
$maxid = 0;
} else {
$maxid = $maxid[0]['tableid'];
}
//验证token
$time = time();
$server_token = md5(md5($key) . md5($time));
$postData = array(
'table_name' => $table,
'each' => $row_per, //每次导入多少条
'maxid' => $maxid,
'time' => $time,
'token' => $server_token,
);
//服务器返回来的数据
$result = json_decode(curl_fun($url, $postData), true