/ Published in: PHP
Author: J. van Hemert
Fixes encoding when utf-8-encoded data
is stored in tables with other (e.g.
latin_swedish_ci) encoding.
Will convert all columns in all tables
to utf8_general_ci.
Fixes encoding when utf-8-encoded data
is stored in tables with other (e.g.
latin_swedish_ci) encoding.
Will convert all columns in all tables
to utf8_general_ci.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php /******************************************/ // db_utf8_fix.php // // Author: J. van Hemert // // // // Fixes encoding when utf-8-encoded data // // is stored in tables with other (e.g. // // latin_swedish_ci) encoding. // // Will convert all columns in all tables // // to utf8_general_ci. // // Run from fileadmin folder in TYPO3 // // installation. // /******************************************/ //Set to TRUE to generate an enormous amount of debug output with //analysis of table structure. //Set to FALSE to really convert the database require_once ('../typo3conf/localconf.php'); ?> <html> <head> <style type="text/css"> .normal { color: black; } .okay { color: green; } .label { color: blue; } .error { color: red; } </style> </head> <body> <?php "char" => "binary", "text" => "blob", ); } } $sql = "SHOW TABLES;"; } $tables[] = $row[0]; } foreach ($tables as $table) { echo "<div><span class=\"label\">".$table.": </span><span class=\"normal\">"; $sql = "SHOW FULL COLUMNS FROM `$table`;"; } if (DEBUG) { echo "column: "; } $columns[] = $row; } foreach ($columns as $column) { $oldtype = $column['Type']; if (DEBUG) echo "Original: ".$column['Type']."\n"; if (DEBUG) echo "modified: ".$column['Type']."\n"; if ($column['Type'] != $oldtype) { $column['Default'] = (is_numeric($column['Default'])) ? $column['Default'] : ($column['Default'] === "NULL")? $column['Default'] : "'{$column['Default']}'"; /* $sql = "ALTER TABLE `$table` MODIFY COLUMN `{$column['Field']}` {$column['Type']} {$column['Null']} DEFAULT {$column['Default']} {$column['Extra']};"; if (DEBUG) { echo "$sql\n"; } else { if (!SIMULATE) { $db_res = mysql_query($sql, $db); if (!is_resource($db_res) && mysql_errno($db) != 0) { echo "Could not execute query!: " . mysql_error($db) . "\n" . $sql; } } } */ $sql = "ALTER TABLE `$table` MODIFY COLUMN `{$column['Field']}` {$column['Type']} CHARACTER SET utf8 COLLATE utf8_general_ci {$column['Null']} DEFAULT {$column['Default']} {$column['Extra']};"; if (DEBUG) { echo "$sql\n"; } else { if (!SIMULATE) { } } } } } $sql = "ALTER TABLE `$table` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"; if (DEBUG) { echo "$sql\n"; } else { if (!SIMULATE) { } } } } echo "<div><span class=\"label\">DATABASE: </span><span class=\"normal\">"; $sql = "ALTER DATABASE `$typo_db` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"; if (DEBUG) { echo "$sql\n"; } else { if (!SIMULATE) { } } } echo "<div>finished converting tables</div>"; ?> </body> </html>