MySQL Datenbank in UTF-8 konvertieren
Um eine Datenbank im laufenden Betrieb in UTF-8 zu konvertieren bedarf es einiger Anpassungen. Das folgende PHP-Skript erleichtert die Konvertierung.
<?php
/* settings */
$db = 'test';
$db_host = 'localhost';
$db_username = 'username';
$db_password = 'password';
/* action */
// connect to db
$dbconection = mysql_connect($db_host, $db_username, $db_password);
if(!$dbconection) {
echo "Error: Database connection to host '" . $db_host . "' can't be established!" . "<br/><br/>";
exit(0);
} else {
echo "Database connection to host '" . $db_host . "' established!" . "<br/><br/>";
}
mysql_select_db($db);
// convert
$result = mysql_query('show tables');
if($result) {
echo "Database '" . $db . "' selected!" . "<br/><br/>";
// convert tables
if(mysql_num_rows($result)) {
while($tables = mysql_fetch_array($result)) {
foreach ($tables as $key => $value) {
$query = "ALTER TABLE " . $value . " CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;";
mysql_query($query);
if (mysql_error()) {
echo mysql_error() . "<br/>";
echo "ERROR: " . $query . "<br/>";
} else {
echo "SUCCESS: " . $query . "<br/>";
}
}
}
}
// set collation to utf8
$query = "ALTER DATABASE dev_aopwebsites DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci";
mysql_query($query);
if (mysql_error()) {
echo mysql_error() . "<br/>";
echo "<br/><br/>" . "ERROR: " . $query . "<br/>";
} else {
echo "<br/><br/>". "SUCCESS: " . $query . "<br/>";
}
echo "<br/><br/><br/>" . "SUCCESS!" . "<br/>";
} else {
echo "<br/><br/><br/>" . "ERROR - Database '" . $db . "' not found!" . "<br/>";
}
?>
Vielen Dank für den obigen Programmcode. Hat auf Anhieb funktioniert.