What happens if I connect to a MySQL utf8 MySQL table using latin1?

Interesting question ... if I have a MySQL table with CHARSET = utf8 and I open a latin encoding connection, what happens?

I tried this and even characters like ß and æ could be saved and retrieved as expected. These characters are represented with different byte sequences in utf8 and latin1, so I didn't expect it to work.

Is MySQL some kind of on-the-fly translation between character encodings?


source to share

2 answers

You will feel like it works as long as you don't connect to a different encoding of the connection. Then you will see strange characters and, of course, regret it.

You are storing bytes in the database that only make sense if you read them using utf8 conversion. But you tell mysql that the field is latin, so it won't understand, but it will send them to you anyway (you are human and he is obbeys). Since you are utf8 related, you will read them through this encoding and read them in order.

But if the connection changes the encoding, or you try to change the encoding of the field later, you run into problems and need tricks like this to read the single content of the field:

convert(cast(convert(FieldNameMessed using latin1) as binary) using utf8)




Yes, mysql does chrset conversion, depending on the configuration. To change the chrset of your connection, you can use the SET NAMES , for example





All Articles