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

修改mysql数据库的名称

PHP中文网
PHP中文网Original
2016-05-25 17:01:391531browse

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;
	}
}

                   

                   

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn