php batch changes database table prefix implementation method

  • 2020-10-31 21:40:24
  • OfStack

This is easy to do with the following method, the code is as follows, useful to the jacking.


<?php
        $database = "databaseName";         // Database name 
        $user = "root";                       // Database user name 
        $pwd = "pwd";                         // Database password 
        $replace ='pre_';                     // Replace the prefix 
        $seach = 'pre1_';                     // The prefix to be replaced 
        $db=mysql_connect("localhost","$user","$pwd") or die(" Failed to connect to database: ".mysql_error());         // Connect to database 
        $tables = mysql_list_tables("$database");        
        while($name = mysql_fetch_array($tables)) {
                $table = str_replace($seach,$replace,$name['0']);

                mysql_query("rename table $name[0] to $table");
        }
?>

If you add a prefix you only have to change it by 1 dot

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

Just fine.


Related articles: