当前位置:Gxlcms > PHP教程 > php批量更改数据库表前缀的方法

php批量更改数据库表前缀的方法

时间:2021-07-01 10:21:17 帮助过:16人阅读

  1. $database = "databaseName"; //数据库名称

  2. $user = "root"; //数据库用户名
  3. $pwd = "pwd"; //数据库密码
  4. $replace ='pre_';//替换后的前缀
  5. $seach = 'pre1_';//要替换的前缀
  6. $db=mysql_connect("localhost","$user","$pwd") or die("连接数据库失败:".mysql_error()); //连接数据库

  7. $tables = mysql_list_tables("$database");

  8. while($name = mysql_fetch_array($tables)) {

  9. $table = str_replace($seach,$replace,$name['0']);

  10. mysql_query("rename table $name[0] to $table");

  11. }
  12. // bbs.it-home.org
  13. ?>

添加前缀的话,可以做如下修改: $table = str_replace($seach,$replace,$name['0']); 修改为: $table = $replace.$name['0'];

人气教程排行