Search code examples
phpmysqlutf-8character-encodinglatin1

What happens when putting UTF-8 data into a latin1 database column?


I'm getting some data which is encoded in UTF-8, which will be Spanish. The database column I need to put it into is currently using the latin1 charset.

What happens if I put that UTF-8 data into those columns? Will I lose any of the correct data? Or should I convert the encoding with the php function mb_convert_encoding?


Solution

  • Every possible byte sequence is valid in latin1, so you could put UTF-8 encoded strings into your column and nothing will complain. When you extract the data back out of the database, the bytes will be the same as when they went in so provided the client knows those bytes are UTF-8 encoded you will get back your original data.

    However, you really shouldn't do it that way: why not change the column data type to UTF-8?

    ALTER TABLE my_table MODIFY my_column TEXT CHARACTER SET utf8;