Typecho文章cid不连续解决方案

① 导语

今天在更新博客文章时发现,文章的cid从31直接跳到了113073874。虽然没什么影响,但是看起来属实无法忍受。
在网上找了很久,终于找到了解决办法。


② 解决方案

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

注意:请在 P H P 7 以 下 版 本 \color{red}{PHP7以下版本} PHP7的环境执行,本操作涉及数据库,请提前做好备份工作。


/**
 * Typecho重新排列不连续的文章ID
 * 作者:http://www.n-1.cn/
 */
 
$hostname_blog = "localhost";
$database_blog = "数据库名";
$username_blog = "数据库用户名";
$password_blog = "数据库密码";
$blog = mysql_pconnect($hostname_blog, $username_blog, $password_blog) or trigger_error(mysql_error(),E_USER_ERROR); 
 
$no = 1;
 
function change_id($cid)
{
    global $no; 
 
    // 修改post cid,并修改分类、标签、自定义字段、评论的对应关系
    $sql = 'update typecho_contents set cid = ' . $no . ' where cid = ' . $cid;
    mysql_query($sql);
    $sql = 'update typecho_relationships set cid = ' . $no . ' where cid = ' . $cid;
    mysql_query($sql);
    $sql = 'update typecho_comments set cid = ' . $no . ' where cid = ' . $cid;
    mysql_query($sql);
 
    $no = $no + 1;
}
 
mysql_select_db($database_blog, $blog);
$query_postRecord = "SELECT cid FROM typecho_contents ORDER BY cid ASC";
$all_postRecord = mysql_query($query_postRecord);
$row_postRecord = mysql_fetch_assoc($all_postRecord);
 
do {
    change_id( $row_postRecord['cid'] );    
} while ($row_postRecord = mysql_fetch_assoc($all_postRecord));
 
// 重新设置post id自增起点
mysql_query('alter table typecho_contents AUTO_INCREMENT = ' . $no);
echo 'ok';
?>

Ps.本人已亲自测试有效,需PHP7.0以下环境才行。出现“ok”以后即可关闭页面并将文件删除。

你可能感兴趣的:(Typecho,php,经验分享)