Created
December 17, 2016 04:46
-
-
Save specktator/def98d0486db1315976b3f5c7e623d56 to your computer and use it in GitHub Desktop.
Change mysql collation from latin to utf8 - all tables and all columns
This file contains 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 | |
/* | |
Description: Change all tables and all columns from latin* to utf8 collation | |
*/ | |
error_reporting(E_ALL); | |
$log = ''; | |
$db = mysql_connect('localhost','user','password'); | |
if(!$db) echo "Cannot connect to the database - incorrect details"; | |
mysql_select_db('project'); $result=mysql_query('show tables'); | |
echo "<pre>"; | |
while($tables = mysql_fetch_array($result)) | |
{ | |
foreach ($tables as $key => $table) | |
{ | |
echo "---------$table------------ \n"; | |
$res = mysql_query("SHOW FULL COLUMNS FROM $table"); | |
while ($columns = mysql_fetch_assoc( $res ) ) | |
{ | |
print_r($columns); | |
foreach ($columns as $ckey => $val) | |
{ | |
if( preg_match('/latin/', $columns['Collation']) != false ) | |
{ | |
echo "changing $val \n"; | |
$query = "ALTER TABLE $table CHANGE ".$columns['Field']." ".$columns['Field']." ".$columns['Type']." CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT ".($columns['Default'] ? $columns['Default'] : 'NULL'); | |
mysql_query($query) or $log += "Can't change table: $table column: $column Collation \n"; | |
} | |
} | |
} | |
} | |
} | |
echo "The collation of your database has been successfully changed!".( !empty($log) ? 'but with errors': ''); | |
if ($log) echo $log; | |
echo "</pre>"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment