'binary', 'varchar' => 'varbinary', 'tinytext' => 'tinyblob', 'text' => 'blob', 'mediumtext' => 'mediumblob', 'longtext' => 'longblob'); // Get next table in list $convert_to_binary = array(); $convert_to_latin1 = array(); $convert_to_utf8 = array(); // Find out which columns need converting and build SQL statements $result = mysql_query('SHOW FULL COLUMNS FROM '. $table .''); while ($column = mysql_fetch_assoc($result)) { list($type) = explode('(', $column['Type']); if (isset($types[$type])) { $names = 'CHANGE `'. $column['Field'] .'` `'. $column['Field'] .'` '; $attributes = ' DEFAULT '. ($column['Default'] == 'NULL' ? 'NULL ' : "'". mysql_real_escape_string($column['Default']) ."' ") . ($column['Null'] == 'YES' ? 'NULL' : 'NOT NULL'); $convert_to_binary[] = $names . preg_replace('/'. $type .'/i', $types[$type], $column['Type']) . $attributes; $convert_to_latin1[] = $names . $column['Type'] .' CHARACTER SET latin1'. $attributes; $convert_to_utf8[] = $names . $column['Type'] .' CHARACTER SET utf8'. $attributes; } } if (count($convert_to_binary)) { //set the table default to latin1 mysql_query('ALTER TABLE '. $table .' DEFAULT CHARACTER SET latin1'); //Convert to latin1 mysql_query('ALTER TABLE '. $table .' '. implode(', ', $convert_to_latin1)); //set the table default to utf8 mysql_query('ALTER TABLE '. $table .' DEFAULT CHARACTER SET utf8'); //Convert latin1 columns to binary mysql_query('ALTER TABLE '. $table .' '. implode(', ', $convert_to_binary)); // Convert binary columns to UTF-8 mysql_query('ALTER TABLE '. $table .' '. implode(', ', $convert_to_utf8)); } }