Skip to content

Instantly share code, notes, and snippets.

@michael-e
Last active July 11, 2020 09:59
Show Gist options
  • Save michael-e/5789168 to your computer and use it in GitHub Desktop.
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.)
<?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();
@ChriStef
Copy link

...thanks for the insides!

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