欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

修改mysql数据库的名称

程序员文章站 2022-04-23 14:49:41
...

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;
	}
}
相关标签: php