Latest news
Home » Database » removing or adding a prefix on existing tables in mysql

removing or adding a prefix on existing tables in mysql

<?php
$db_password = ‘root’;
$db_user = ‘root’;
$db_host = “-h localhost”; // Probably localhost
$db_name = ‘test’;
$prefix =’smarttips_’;
$remove = ‘removeprefix’;//set a a condition name
if(strtolower($remove) == ‘removeprefix’ || strtolower($remove) == ‘remove’){
$removeprefix = (bool) TRUE;
}
else {
$removeprefix = (bool) FALSE;
}

$renmeQuery = “mysql -u$db_user -B -p$db_password -s -r $db_host $port $db_name -e ‘SHOW TABLES;'”;
exec($renmeQuery, $tables);

if($removeprefix){
foreach($tables as $key => $t){
$aRmoveprefix[$key][‘from’] = $t;
$aRmoveprefix[$key][‘to’] = preg_replace(‘/^’.$prefix.’/i’,”,$t);
}
}
else {
foreach($tables as $key => $t){
$aRmoveprefix[$key][‘from’] = $t;
$aRmoveprefix[$key][‘to’] = $prefix . $t;
}
}
foreach($aRmoveprefix as $renamePrefix){
$renmeQuery = “mysql -u$db_user -B -p$db_password -s -r $db_host $port $db_name -e ‘RENAME TABLE $db_name.{$renamePrefix[‘from’]} TO $db_name.{$renamePrefix[‘to’]};'”;
exec($renmeQuery);
}
exit(‘Table prefixes are Removed Successfully…’);
?>

Leave a Reply

x

Check Also

How to print Codeigniter upload error

if you want to display errors on upload functionality in codeigniter. Use the following function ...