Skip to content

Instantly share code, notes, and snippets.

@unfulvio
Last active January 13, 2021 12:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unfulvio/1604757b757dc196b3b4 to your computer and use it in GitHub Desktop.
Save unfulvio/1604757b757dc196b3b4 to your computer and use it in GitHub Desktop.
PHP Script to alter and convert all tables in a MySQL database to use a different character set and collation
<?php
// enter your database name
$database_name = 'name';
// enter your database user name
$database_username = 'user';
// enter your database user password
$database_password = 'password';
$connection = mysql_connect( 'localhost', $database_username, $database_password );
if ( !$connection ) {
echo 'ERROR - Cannot establish connection to database!';
} else {
mysql_select_db( $database_name );
$result = mysql_query( 'show tables' );
while( $tables = mysql_fetch_array( $result ) ) :
foreach ( $tables as $key => $value ) :
// you can change character set and collation here by replacing lowercase values with the ones you want
mysql_query("ALTER TABLE ".$value." CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci");
endforeach;
endwhile;
echo 'Done!';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment