首页 >php教程 >PHP源码 >修改mysql数据库的名称

修改mysql数据库的名称

PHP中文网
PHP中文网原创
2016-05-25 17:01:391530浏览

1. [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;
	}
}

                   

                   

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn