Home  >  Article  >  Backend Development  >  How to batch change database table prefix in php

How to batch change database table prefix in php

WBOY
WBOYOriginal
2016-07-25 08:57:41888browse
  1. $database = "databaseName"; //Database name

  2. $user = "root"; //Database user name
  3. $pwd = "pwd"; //Database Password
  4. $replace ='pre_';//Prefix after replacement
  5. $seach = 'pre1_';//Prefix to be replaced
  6. $db=mysql_connect("localhost","$user","$pwd") or die("Failed to connect to database:".mysql_error()); //Connect to database

  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. ?>

Copy the code

If you add a prefix, you can make the following modifications: $table = str_replace($seach,$replace,$name['0']); change into: $table = $replace.$name['0'];



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn