====== Convert latin1 MySQL db to utf8 ======
Export db, change charset, convert and import again:
mysqldump -u username -p --add-drop-table database-name | replace CHARSET=latin1 CHARSET=utf8 | iconv -f latin1 -t utf8 | mysql -u username -p -D database-name
Directly via mysql, note utf8 charset is not fully compatible with UTF-8, so better use utf8mb4:
#change default only
ALTER TABLE tablename CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
#convert charset of data
ALTER TABLE tablename convert to character set utf8mb4 COLLATE utf8mb4_unicode_ci;
#changes for whole database
ALTER DATABASE databasename CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER DATABASE databasename convert to character set utf8mb4 COLLATE utf8mb4_unicode_ci;
For websites, ensure that the header and document type are set correctly:
header('Content-Type: text/html; charset=UTF8');
php.ini should have:
; PHP's built-in default is text/html
default_mimetype = "text/html"
default_charset = "utf-8"
my.cnf should have:
[client]
default-character-set=utf8
[myslqd]
skip-character-set-client-handshake
collation_server=utf8_unicode_ci
character_set_server = utf8
alternatively, in php scripts set:
@mysql_query("SET NAMES utf8") or die ("Could not set character set to UTF8: ".mysql_error());