Skip to content

Instantly share code, notes, and snippets.

@ChriStef
Forked from michael-e/mysql-alter-tables.php
Last active July 11, 2020 09:59
Show Gist options
  • Save ChriStef/4028cc3b5bd728bcb3ce5787c1ffaf52 to your computer and use it in GitHub Desktop.
Save ChriStef/4028cc3b5bd728bcb3ce5787c1ffaf52 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
$link = mysqli_connect("host","username","password");
mysqli_select_db($link, "database");
// do it
$resource = mysqli_query("SHOW TABLES");
while ($row = mysqli_fetch_array($resource)) {
$table = $row[0];
$query = mysqli_query($link, 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
Author

...changed to work >php v.7

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