|
Posted by NC on 07/18/07 23:06
On Jul 12, 3:44 pm, 2...@virgin.co.uk wrote:
>
> I need to rename all the tables in my databases by changing
> the first few characters of each table name:
>
> eg
> my_email to email
> my_zips to zip
$oldPrefix = 'my_';
$newPrefix = '';
// Connect to the server and choose the database, and then...
$result = mysql_query('SHOW TABLES');
while ($record = mysql_fetch_row($result)) {
$oldName = $record[0];
$newName = str_replace($oldPrefix, $newPrefix, $oldName);
mysql_query("RENAME TABLE $oldName TO $newName");
}
Note that using str_replace() may produce unexpected results (for
example, my_infamy_table would be renamed to infatable). If this is a
concern, you should use a regular expression instead of
str_replace().
Best regards,
NC
Navigation:
[Reply to this message]
|