• notice
  • Congratulations on the launch of the Sought Tech site

Experience summary about mysql encoding problems

The following description has no theoretical basis and is purely empirical.

MySQL uses version 4.1 or later, no matter what character set it is, it will always use the default.No need to set up MySQL.

Then give an example using GB2312 and UTF-8.

Okay, you just need to make sure that your PHP file with the INSERT SQL statement is encoded as GB2312, congratulations , the data you read using the GB2312 encoded PHP file with the SELECT SQL statement is also GB2312.
In the same way, as long as the PHP file you insert into the database is UTF-8 encoded, then what you enter is UTF-8, and the read out is also encoded as UTF-8.

What should I do if I enter GB2312 and it shows that I want to use UTF-8?

If all your PHP files are UTF-8 encoded, then you must use iconv for encoding conversion when INSERT, convert str to GB2312 for storage, and read the same, use iconv to convert to UTF-8 show.

What encoding does your front-end page use, then your strings have already been encoded by that encoding, so even though it is stored in the library, his machine code must be like that, no matter where it is stored, no matter what MYSQL specifies Encoding, when he enters data, does not convert your data.As long as you make sure that your front-end input uses UTF-8, then you are also reading UTF-8.

Why is there always a problem with importing with PHPMYADMIN?

I haven't studied the working principle of PHPMYADMIN, but his PHP files are all UTF-8 encoded, that is, the imported data are all UTF-8, you use GB2312 PHP files to read, of course a bunch of chaos.
I guess there are three solutions:

When reading, use SET NAME to convert its encoding, I don't know if it is feasible,

Use iconv to convert it to GB2312 after reading,

Do not import PHPMYADMIN, just write a GB2312 PHP script to import.

In a word, what code is used when you enter it, then what code is it when you read it.If you want to display this encoding as another encoding, then you need to use iconv or mbstring to encode it.

The above is just my personal experience, I don't have time to study the theory.Welcome to discuss or correct me if I have any mistakes.


Technical otaku

Sought technology together

Related Topic


Leave a Reply