Skip to content

Instantly share code, notes, and snippets.

@sanjaybhowmick
Created September 10, 2015 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save sanjaybhowmick/57b2cdf96d952d207a5d to your computer and use it in GitHub Desktop.
Save sanjaybhowmick/57b2cdf96d952d207a5d to your computer and use it in GitHub Desktop.
Convert MySQL collation from utf8mb4 to utf8
<?php
$dbname = 'your-database-name';
mysql_connect('your-database-hostname', 'your-database-username', 'your-database-password');
mysql_query("ALTER DATABASE `$dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci");
$result = mysql_query("SHOW TABLES FROM `$dbname`");
while($row = mysql_fetch_row($result)) {
$query = "ALTER TABLE {$dbname}.`{$row[0]}` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci";
mysql_query($query);
$query = "ALTER TABLE {$dbname}.`{$row[0]}` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci";
mysql_query($query);
}
echo 'All the tables have been converted successfully';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment