Skip to content

Instantly share code, notes, and snippets.

@pepebe
Created January 17, 2013 09:52
Show Gist options
  • Save pepebe/4554926 to your computer and use it in GitHub Desktop.
Save pepebe/4554926 to your computer and use it in GitHub Desktop.
SQL: Repair German "Umlaute" inside a MySQL Database.
<?php
/**
* Alle kaputten Umlaute reparieren bei Umstellung von ISO->UTF8
* Source: http://xhtmlforum.de/66480-kleines-skript-alle-umlaute-der-datenbank.html
*
* @project -
* @author Boris Bojic <[email protected]>
* @copyright Copyright (c) 2011, Boris Bojic (DevShack)
* @version Fri, 23 Dec 2011 13:47:11 +0100
* @updated -
*
*/
// === [ Content / Charset ] ==============================================
header('Content-Type: text/html; charset=utf-8');
// PHP auch explizit auf UTF-8 setzen
mb_internal_encoding('UTF-8');
$db = array();
$db['host'] = "localhost";
$db['uname'] = "mysql_user";
$db['password'] = "mysql_pass";
$db['database'] = "datenbankname";
$dbconnect = mysql_connect($db['host'], $db['uname'], $db['password']) or die ("Konnte keine Verbindung zur Datenbank aufnehmen!");
mysql_select_db($db['database'],$dbconnect) or die ("Fehler beim Auswählen der Datenbank!");
mysql_set_charset('utf8');
echo '<pre>';
function getTables($db){
$result = mysql_query("SHOW TABLES FROM " . $db['database']);
while($row = mysql_fetch_row($result)){
$res[] = $row[0];
}
return $res;
}
function getColumns($table){
$table = mysql_real_escape_string($table);
$mysqlres = mysql_query("SHOW COLUMNS FROM " . $table);
while($row = mysql_fetch_row($mysqlres)){
$res[] = $row[0];
}
return $res;
}
// Alle Tabellen ermitteln
$tablesArray = getTables($db);
// Alle Spalten pro Tabelle ermitteln und durcharbeiten
foreach($tablesArray AS $table){
$affectedRows = 0;
$spalten = getColumns($table);
echo "Tabelle: " . $table . "<br />";
foreach($spalten AS $spalte){
echo "...Spalte: " . $spalte . "<br />";
$query = '
UPDATE `' . $table . '` SET
`' . $spalte . '` = REPLACE(`' . $spalte . '`,"ß", "ß"),
`' . $spalte . '` = REPLACE(`' . $spalte . '`, "ä", "ä"),
`' . $spalte . '` = REPLACE(`' . $spalte . '`, "ü", "ü"),
`' . $spalte . '` = REPLACE(`' . $spalte . '`, "ö", "ö"),
`' . $spalte . '` = REPLACE(`' . $spalte . '`, "Ä", "Ä"),
`' . $spalte . '` = REPLACE(`' . $spalte . '`, "Ãœ", "Ü"),
`' . $spalte . '` = REPLACE(`' . $spalte . '`, "Ö", "Ö"),
`' . $spalte . '` = REPLACE(`' . $spalte . '`, "€", "€")
';
mysql_query($query) OR die(mysql_error() . $query);
$affectedRows += mysql_affected_rows();
}
echo "Tabelle " . $table . " aktualisiert, Datensätze: " . $affectedRows . "<br /><br />";
}
@CodeBrauer
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment