imvkmark
10/7/2013 - 5:06 AM

php:alterDatabaseName

php:alterDatabaseName

<?php
$setting = array(
    'databaseHost'    => '127.0.0.1',
	'databaseUser'    => 'root',
	'databasePass'    => '',
	'oldDatabaseName' => 'old_database_name',
	'newDatabaseName' => 'new_database_name',
	'execute'         => false,
);
$db      = new PDO("mysql:host={$setting['databaseHost']};dbname={$setting['oldDatabaseName']}", $setting['databaseUser'], $setting['databasePass']);
$result  = $db->query("show tables");
$tables  = $result->fetchAll();
foreach ($tables as $tableCtr) {
	$tablename = $tableCtr['Tables_in_' . $setting['newDatabaseName']];
	$sql       = "ALTER TABLE $tablename RENAME TO " . str_replace('mymps', 'mk_', $tablename);
	$sql       = "RENAME TABLE {$setting['oldDatabaseName']}.{$tablename} TO {$setting['newDatabaseName']}.{$tablename};";
	if ($setting['execute']) {
		if ($db->query($sql)) {
			continue;
		} else {
			exit($db->errorInfo());
		}
	} else {
		echo $sql . PHP_EOL;
	}
}