网站首页
PHP 批量修改表名前缀的屌法
发布时间:2017-07-15 23:57:51编辑:admin阅读()
<?php
$old_pre = @$_GET['o']; // 原表前缀
$new_pre = @$_GET['n']; // 新表前缀
$length = strlen($old_pre); // 原表前缀长度
// 连接数据库
$mysqli = @new mysqli("localhost","root","root","gouwu");
if(mysqli_connect_errno())
{
exit ("连接数据库失败:".mysqli_connect_error());
}
// 查询当前数据库中所有的表
$result = $mysqli->query('SHOW TABLES');
echo "-- <pre>\n\r";
// 遍历所有的表
while($row = $result->fetch_row())
{
$old_tablename = $row[0];
// 判断表名是否包含要修改前缀
if($old_pre && $old_pre == substr($old_tablename, 0, $length))
{
$new_tablename = $new_pre.substr($old_tablename, $length);
echo " ALTER TABLE `{$old_tablename}` RENAME TO `{$new_tablename}`; \n\r";
}
}
echo "-- </pre>";
$mysqli->close();访问连接,得出SQL。直接执行
http://127.0.0.1/t.php?o=旧的前缀&n=新的前缀
相关文章