Commit 8965b783 authored by 于飞's avatar 于飞

文章推送脚本

parent f51bb3a8
<?php
// 连接源数据库
$sourceDb = new PDO('mysql:host=39.105.143.94;port=3366;dbname=db_szts_szzx_ai', 'szts_szzx_ai', 'QyekZnXpWBSbEK5');
// 连接目标数据库
$targetDb = new PDO('mysql:host=39.105.143.94;port=3366;dbname=db_szts_szzx', 'root', 'iA1^cE5%cL');
while (true) {
// 从源数据库读取信息
$stmt = $sourceDb->query('SELECT * FROM articles where status = 0 limit 1');
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
// 插入到目标数据库
foreach ($rows as $row) {
// 去重标题 如果目标库已有该标题的文章 则跳过
$title = $row['title'];
$query = $targetDb->query("SELECT * FROM fa_cms_archives WHERE title = '$title'");
$count = $query->rowCount();
if ($count > 0) {
$stmt = $sourceDb->prepare("UPDATE articles SET status = 2 WHERE id = :id");
$stmt->execute(['id' => $row['id']]);
break;
}
// 拼接sql
$fields = "user_id, channel_id, model_id, title, image, weigh, views, comments, likes, dislikes";
$values = [
'user_id' => 0,
'channel_id' => 25,
'model_id' => 1,
'title' => $row['title'],
'image' => $row['images'],
'weigh' => 0,
'views' => 0,
'comments' => 0,
'likes' => 0,
'dislikes' => 0
];
$id = $row['id'];
// 新增到目标库
$stmt = $targetDb->prepare("INSERT INTO fa_cms_archives ($fields) VALUES (:user_id, :channel_id, :model_id, :title, :image, :weigh, :views, :comments, :likes, :dislikes)");
$stmt->execute($values);
$insertId = $targetDb->lastInsertId();
// 更新原库状态
$stmt = $sourceDb->prepare("UPDATE articles SET status = 1, cms_id = :insertId WHERE id = :id");
$stmt->execute(['insertId' => $insertId, 'id' => $id]);
$stmt = $targetDb->prepare("INSERT INTO fa_cms_addonnews (`id`, `content`) VALUES (:id, :content)");
$stmt->execute([':id' => $insertId, ':content' => $row['content']]);
}
// 休眠1000毫秒
usleep(10000000);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment