PHP and Web Technologies Freak
Sometimes, especially when moving data from one server to another, you might find that you have your MySQL database incorrectly encoded. Problem with this first show itself if you have the database and encoded in one charset set to display your website in another. If this is the case then you will find strange characters appearing in your text, especially when using punctuation marks. If you are Unwilling or unable to change the character encoding on the site then you need to change how the data is encoded in the database.
The most common sort of thing you might want to do is change from iso-8859-1 (or windows-1252) to UTF-8. This can be done in one of two ways.
The first way is to simply alter the table so that the column contains a different charset.
ALTER TABLE table MODIFY col1 VARCHAR(50) CHARACTER SET 'utf8';
However, if your database has already been set up and your data has already been inserted in the wrong format then you can also update the data in the column using the CONVERT command. The following snippet turns our latin1 data into uncoded binary data and then into utf8.
UPDATE table SET col1=CONVERT(CONVERT(CONVERT(col1 USING 'latin1') USING BINARY) USING 'utf8');
You should also make sure that the connection to the database is done through a specific character set. This is done by using the
SET NAMES command and the SET CHARACTER SET. SET NAMES 'charset_name' SET CHARACTER SET 'charset_name';
These two commands basically set some values in your MySQL database, for more information on what is set look at the Connection Character Sets and Collations page on the MySQL website. This ensures that the data we get back from the database is also in the correct charset.
For a full list of the different character sets available in MySQL just run the command:
SHOW CHARACTER SET;
This will display a table with the columns Charset, Description, Default collation and Maxlen. Each charset is associated with a collation. A collation is a set of rules for comparing characters in a charset, so it is important that you get this right if you want the database to work. The full list of collations can be viewed using the following command:
SHOW COLLATION;
You can even use a LIKE statement to refine the collation data into the information you are looking for.
SHOW COLLATION WHERE Charset LIKE '%utf%'
ozan
July 9th, 2009 at 18:15
selam benim bu karakter setleri ile ilgili problemlerim var yardım edebilirmisin?
veteriner
April 9th, 2010 at 17:55
excellent work
thanks for sharin
hemen deniyorum çok lazımdı
veteriner
April 12th, 2010 at 16:20
hi,
after ive tried a few times this worked fine for me,
thanks for the idea,
CONVERT(CONVERT(CONVERT(CONVERT(adi USING ‘latin1′) USING BINARY) USING ‘latin5′) USING ‘utf8′)
mutlaka önce bi select ile sonucu deneyin derim ben, ardından update yi kullanın
bütün yükten kurtardı beni, çok saol
admin
April 12th, 2010 at 17:31
i m glad to it helps
have a good day
tobi
February 22nd, 2011 at 17:40
U are my men!
Scott Park
March 13th, 2011 at 07:00
Thanks for the really helpful post. This saved me tons of time when correcting a botched import from latin1 to utf-8.