MENU

解决Typecho文章cid、分类和标签mid不连续的问题

August 1, 2024 • Read: 34 • Typecho

php7.2可用

解决cid不连续的问题

将以下代码保存为PHP文件并上传至网站根目录,在地址栏访问一下即可。

<?php
/**
* Typecho重新排列分文章cid让他连续
*/
<?php

// 设置数据库参数
$hostname_blog = "localhost";
$database_blog = "数据库名";
$username_blog = "数据库用户名";
$password_blog = "数据库密码";

// 连接到数据库
$blog = mysqli_connect($hostname_blog, $username_blog, $password_blog, $database_blog) or                 die(mysqli_connect_error());

// 设置计数器的初始值
$no = 1;

// 定义一个函数来更新文章cid
function change_id($cid)
{
global $no, $blog;

// 更新文章cid,并更新与类别、标签、自定义字段和评论的关系
mysqli_query($blog, "UPDATE typecho_contents SET cid = $no WHERE cid = $cid");
mysqli_query($blog, "UPDATE typecho_relationships SET cid = $no WHERE cid = $cid");
mysqli_query($blog, "UPDATE typecho_comments SET cid = $no WHERE cid = $cid");

$no = $no + 1;
}

// 从数据库中选择所有文章cid,按其 cid 排序
$query_postRecord = "SELECT cid FROM typecho_contents ORDER BY cid ASC";
$result = mysqli_query($blog, $query_postRecord);

// 循环浏览文章并更新其 cid
while ($row = mysqli_fetch_assoc($result)) {
change_id($row['cid']);
}

// 重置文章 cid 的自动递增值
mysqli_query($blog, "ALTER TABLE typecho_contents AUTO_INCREMENT = $no");

echo 'ok';

?>

解决分类和标签mid不连续的问题

<?php
/**
* Typecho重新排列分类和标签(meta)不连续的mid
*/

// 数据库参数
$hostname_blog = "localhost";
$database_blog = "数据库名";
$username_blog = "数据库用户名";
$password_blog = "数据库密码";
$blog = new mysqli($hostname_blog, $username_blog, $password_blog, $database_blog);

// 检查错误
if ($blog->connect_error) {
trigger_error($blog->connect_error, E_USER_ERROR);
}

// 定义全局变量以跟踪“no”值
$no = 1;

// 定义一个函数来更新 mid 及其关系
function change_id($mid)
{
global $no;
global $blog;

// 更新mid
$sql = "UPDATE typecho_metas SET mid = ? WHERE mid = ?";
$stmt = $blog->prepare($sql);
$stmt->bind_param("ii", $no, $mid);
$stmt->execute();

// 更新关系
$sql = "UPDATE typecho_relationships SET mid = ? WHERE mid = ?";
$stmt = $blog->prepare($sql);
$stmt->bind_param("ii", $no, $mid);
$stmt->execute();

// 递增“no”值
$no++;
}

// 从数据库中获取所有mid
$query_postRecord = "SELECT mid FROM typecho_metas ORDER BY mid ASC";
$all_postRecord = $blog->query($query_postRecord);

// 循环遍历结果并调用 change_id() 函数
while ($row_postRecord = $all_postRecord->fetch_assoc()) {
change_id($row_postRecord['mid']);
}

// 重置typecho_metas表的自动增量值
$blog->query("ALTER TABLE typecho_metas AUTO_INCREMENT = $no");

echo 'ok';
?>


Last Modified: August 4, 2024
Archives QR Code Tip
QR Code for this page
Tipping QR Code