Last active
January 13, 2021 12:57
-
-
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
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 | |
// 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