Last active
July 11, 2020 09:59
-
-
Save michael-e/5789168 to your computer and use it in GitHub Desktop.
Loop over all tables of a MySQL database and convert them to `utf8` character set. Also make them use `utf8_unicode_ci` collation. You are encouraged to use the PHP CLI (i.e. run the script from the command line), simply because it may take a while. (On the webserver the script may timeout.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// configuration | |
mysql_connect("host","username","password"); | |
mysql_select_db("database"); | |
// do it | |
$resource = mysql_query("SHOW TABLES"); | |
while ($row = mysql_fetch_array($resource)) { | |
$table = $row[0]; | |
$query = mysql_query(sprintf( | |
"ALTER TABLE `%s` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci", | |
$table | |
)); | |
if ($query === true) { | |
echo $table . ": OK\r\n"; | |
} | |
else { | |
echo $table . ": ERROR\r\n"; | |
} | |
} | |
exit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
...thanks for the insides!