解决Typecho文章cid不连续的问题PHP代码 兼容php8.2 2025/6
丢进chatgpt
提示词:改为PHP 8.2兼容
https://forum.typecho.org/viewtopic.php?t=10522
存为xxx.php
放进typecho根目录
执行网址http://typecho目录/xxx.php
2025/6/6 测试通过
执行后显示OK
// 解决Typecho文章cid不连续
<?php
$hostname_blog = "localhost";
$database_blog = "你的数据库名称如typecho";
$username_blog = "你的数据库管理员如root";
$password_blog = "你的密码如password";
// 创建数据库连接
$blog = new mysqli($hostname_blog, $username_blog, $password_blog, $database_blog);
// 检查连接
if ($blog->connect_error) {
die("连接失败: " . $blog->connect_error);
}
$no = 1;
function change_id($cid, $mysqli, &$no)
{
// 使用准备好的语句更新 post cid,并修改分类、标签、自定义字段、评论的对应关系
$stmt = $mysqli->prepare('UPDATE typecho_contents SET cid = ? WHERE cid = ?');
$stmt->bind_param('ii', $no, $cid);
$stmt->execute();
$stmt = $mysqli->prepare('UPDATE typecho_relationships SET cid = ? WHERE cid = ?');
$stmt->bind_param('ii', $no, $cid);
$stmt->execute();
$stmt = $mysqli->prepare('UPDATE typecho_comments SET cid = ? WHERE cid = ?');
$stmt->bind_param('ii', $no, $cid);
$stmt->execute();
$no++;
}
$query_postRecord = "SELECT cid FROM typecho_contents ORDER BY cid ASC";
$result = $blog->query($query_postRecord);
if ($result) {
while ($row_postRecord = $result->fetch_assoc()) {
change_id($row_postRecord['cid'], $blog, $no);
}
}
// 重新设置 post id 自增起点
$blog->query('ALTER TABLE typecho_contents AUTO_INCREMENT = ' . $no);
echo 'ok';
// 关闭数据库连接
$blog->close();
?>
https://blog.xl0408.top/index.php/archives/33/
// Typecho重新排列分类和标签(meta)不连续的mid
<?php
/**
* Typecho重新排列分类和标签(meta)不连续的mid
*/
// 数据库参数
$hostname_blog = "localhost";
$database_blog = "typecho";
$username_blog = "root";
$password_blog = "";
$blog = new mysqli($hostname_blog, $username_blog, $password_blog, $database_blog);
// 检查连接错误
if ($blog->connect_errno) {
throw new RuntimeException("连接数据库失败: " . $blog->connect_error);
}
// 定义全局变量以跟踪“no”值
$no = 1;
// 定义一个函数来更新 mid 及其关系
function change_id($mid)
{
global $no, $blog;
// 更新mid
$sql = "UPDATE typecho_metas SET mid = ? WHERE mid = ?";
if ($stmt = $blog->prepare($sql)) {
$stmt->bind_param("ii", $no, $mid);
$stmt->execute();
$stmt->close();
} else {
throw new RuntimeException("准备 SQL 失败: " . $blog->error);
}
// 更新关系
$sql = "UPDATE typecho_relationships SET mid = ? WHERE mid = ?";
if ($stmt = $blog->prepare($sql)) {
$stmt->bind_param("ii", $no, $mid);
$stmt->execute();
$stmt->close();
} else {
throw new RuntimeException("准备 SQL 失败: " . $blog->error);
}
// 递增“no”值
$no++;
}
// 从数据库中获取所有mid
$query_postRecord = "SELECT mid FROM typecho_metas ORDER BY mid ASC";
if ($all_postRecord = $blog->query($query_postRecord)) {
// 循环遍历结果并调用 change_id() 函数
while ($row_postRecord = $all_postRecord->fetch_assoc()) {
change_id($row_postRecord['mid']);
}
$all_postRecord->free();
} else {
throw new RuntimeException("查询失败: " . $blog->error);
}
// 重置typecho_metas表的自动增量值
if (!$blog->query("ALTER TABLE typecho_metas AUTO_INCREMENT = $no")) {
throw new RuntimeException("重置自动增量值失败: " . $blog->error);
}
echo 'ok';
?>