DeTechn Blog

PHP MYSQL 批量修改表前缀

<?php
/*
 * MYSQL 批量修改表前缀
*/
error_reporting(0);
$old_pre = 'tdr_'; // 原表前缀
$new_pre = 'db_'; // 新表前缀
// 配置连接
$db = new mysqli('127.0.0.1', 'db_user', 'db_pass', 'db_name');
if ($db-&gt;connect_error)&nbsp;die(&#39;Connect&nbsp;Error&nbsp;(&#39;&nbsp;.&nbsp;$db->connect_errno . ') ' . $db->connect_error);

$query&nbsp;=&nbsp;$db->query('SHOW TABLES;');
$list&nbsp;=&nbsp;$query->fetch_all();
echo 'table : ', count($list),'<hr>';

$len&nbsp;=&nbsp;strlen($old_pre);
foreach ($list&nbsp;as&nbsp;$val) {
    $new_name&nbsp;=&nbsp;$new_pre.substr($val[0],&nbsp;$len, strlen($val[0])-$len);
    echo "ALTER TABLE {$val[0]}&nbsp;RENAME&nbsp;{$new_name};<br />";
    $db-&gt;query(&quot;ALTER&nbsp;TABLE&nbsp;{$val[0]} RENAME {$new_name};");
}
echo '<hr>success.';

$db->close();
?>

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »