Last active
December 14, 2015 01:28
-
-
Save osrecio/5006139 to your computer and use it in GitHub Desktop.
Rename Wordpress Database Prefix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function changePrefixWP ($old_pref,$new_pref,$server,$user,$pass,$bd) { | |
//Connect with DB | |
$link = mysql_connect($server, $user, $pass); | |
if (!$link) { | |
die('Error: ' . mysql_error()); | |
} | |
//Get the table resource | |
$result = mysql_list_tables($bd); | |
//Count the number of tables | |
$num_rows = mysql_num_rows($result); | |
//Rename all the tables name | |
for ($i = 0; $i < $num_rows; $i++) { | |
//Get name old table name | |
$table_old_name = mysql_tablename($result, $i); | |
//Set name new table name | |
$table_new_name = $new_pref . substr($table_old_name, strlen($old_pref)); | |
$sql = "RENAME TABLE $table_old_name TO $table_new_name"; | |
if (mysql_query($sql)) { | |
echo "ok: ".$sql."<br>"; | |
}else { | |
echo "error: ".$sql."<br>"; | |
} | |
} | |
//Fix other tables | |
//Select Database | |
if (!mysql_select_db($bd)) { | |
echo "No Database Selected: " . mysql_error(); | |
exit; | |
} | |
//wp_options | |
$sql = 'UPDATE '.$new_pref.'options SET option_name="'.$new_pref.'user_roles" WHERE option_name LIKE "'.$old_pref.'user_roles"'; | |
if (mysql_query($sql)) { | |
echo "ok: ".$sql."<br>"; | |
}else { | |
echo "error: ".$sql."<br>"; | |
} | |
//wp_usermeta | |
$sql = 'SELECT * FROM '.$new_pref.'usermeta WHERE meta_key LIKE "'.$old_pref.'%"'; | |
$result = mysql_query($sql,$link) or die("Error en: $sql: " . mysql_error()); | |
while ($row = mysql_fetch_array($result)) { | |
$sql = 'UPDATE '.$new_pref.'usermeta SET meta_key="'.$new_pref.substr($row['meta_key'], strlen($old_pref)).'" WHERE meta_key LIKE "'.$old_pref.substr($row['meta_key'], strlen($old_pref)).'"'; | |
//echo $sql."<br>"; | |
if (mysql_query($sql)) { | |
echo "ok: ".$sql."<br>"; | |
}else { | |
echo "error: ".$sql."<br>"; | |
} | |
} | |
//Close connection | |
mysql_close($link); | |
} | |
$old_prefix = 'pru_'; | |
$new_prefix = 'lib_'; | |
$server = 'localhost'; | |
$user = 'root'; | |
$pass = 'root'; | |
$bd = 'libelium_alpha'; | |
changePrefixWP($old_prefix,$new_prefix,$server,$user,$pass,$bd); | |
echo "The End..."; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment