wer einen mysql collation mix hat und diese per php script für alle Tabellen und Datenfelder vereinheitlichen möchte, der sollte dieses script testen. hierbei werden alle Tabellen und Datenfelder auf die collation “latin1″ umgestellt.
<?php
$link = mysql_connect(“localhost”, “USER”, “PASSWORT”);
$db_selected = mysql_select_db(“DATENBANK”, $link);
if (!$db_selected) {
die (‘Kann testfall2 nicht benutzen : ‘ . mysql_error());
}# Do not change anything below this (
<- Rewwrite Editors note.)
$sql = ‘SHOW TABLES’;
if ( !( $result = mysql_query( $sql ) ) ) {
echo ‘<span style=”color: red;”>Get SHOW TABLE – SQL Error: <br>’ . “</span>\n”;
}while ( $tables = mysql_fetch_array($result) ) {
echo “Aktuelle Tabelle ist: ” . $tables[0] . “<br>\n”;
# Loop through all tables in this database
$table = $tables[key($tables)];if ( !( $result2 = mysql_query(“ALTER TABLE `$table` DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci”) ) ) {
echo ‘<span style=”color: red;”>LATIN SET – SQL Error: <br>’ . “</span>\n”;break;
}print “$table changed to Latin1 successfully.<br>\n”;
# Now loop through all the fields within this table
if ( !($result2 = mysql_query(“SHOW COLUMNS FROM `$table`”) ) ) {
echo ‘<span style=”color: red;”>Get Table Columns Query – SQL Error: <br>’ . “</span>\n”;break;
}
while ( $column = mysql_fetch_array( $result2 ) )
{
$field_name = $column['Field'];
$field_type = $column['Type'];# Change text based fields
$skipped_field_types = array(‘char’, ‘text’, ‘enum’, ‘set’);foreach ( $skipped_field_types as $type )
{
if ( strpos($field_type, $type) !== false )
{
$sql4 = “ALTER TABLE `$table` CHANGE `$field_name` `$field_name` $field_type CHARACTER SET latin1 COLLATE latin1_general_ci”;
$result4 = mysql_query($sql4);echo “—- $field_name changed to Latin1 successfully.<br>\n”;
}
}
}
echo “<hr>\n”;
}
echo mysql_errno() . “: ” . mysql_error(). “\n”;
mysql_close();
?>
Moin!!
Vielen Dank für diesen code! Hat mir ein zwei Stunden arbeit gespart. Ich muss eine komplette DB von Latin1 auf UTF8 umbaun, dafür nutze ich Dein script und änder es an manchen stellen ab!!
Vielen Dank!
Gruß,
Daniel
das freut mich
Hallo, leider lässt sich der Code so schlecht kopieren. Eine Downloadmöglichkeit wäre nett.