Home>Article>Database> How to solve the problem of garbled characters in mysql

How to solve the problem of garbled characters in mysql

藏色散人
藏色散人 Original
2020-11-02 09:59:27 1989browse

The solution to garbled characters in mysql: first open the server\MySQL folder; then open the my.ini file; and finally add the code "character_set_server=gbk".

How to solve the problem of garbled characters in mysql

Recommended: "mysql video tutorial"

java website MySQL database individual text garbled problem

The problem of garbled characters is very troublesome, especially a very small amount of garbled characters. Here I would like to share a garbled code problem encountered during personal development. I hope it will be helpful to everyone.

Methods/Steps

Check the location where garbled characters appear - the front desk.

Here the editor checked the front-end jsp page through the firebug tool of Firefox browser and found that the post was indeed utf8 data. Therefore, it is excluded that garbled characters are generated when transmitting data in the foreground.

Check the location where the garbled characters appear - the background.

Because my colleagues use struts formbean to receive front-end data, the editor doesn't know much about it, so I used a stupid method: find the data transmission process that generates garbled code, and check each method passed in the process. Add an output and view the output data in the background.

As a result, these data are also normal.

Check the location where the garbled characters appear - MySQL database.

When the front and backend are excluded, only the database is left. Then the problem lies with the database.

The editor first directly recorded the garbled Chinese characters in the database and found that it can be stored. The problem of text encoding of database tables has been eliminated.

Afterwards, the editor queried it in the tool software (statement: show variables like '%char%';), and the result is as shown below. The problem was immediately discovered: Since the Chinese characters in gb2312 are not complete, some Chinese characters are not encoded. At this time, these Chinese characters will be forcibly converted into "?". At this point, the data of this Chinese character is lost and cannot be restored.

How to solve the problem of garbled characters in mysql

Detailed explanation of the reason:

There are actually several conversion procedures for data entering and exiting the MySQL database.

When entering: character_set_client to character_set_connection to character_set_server and stored in the database.

When taking out: the database is taken out and converted to character_set_server to character_set_result.

You should understand that the data entering and exiting the database must be converted by character_set_server, so whether it is the correct Chinese character in the front desk or the correct Chinese character in the database, as long as it is not in gb2312, it will be converted into a question mark.

Solution:

A once and for all method, the editor directly modified the my.ini file in the server\MySQL folder.

The modification process is: find [mysqld] and then search a few lines down to find "default_character_set=utf8", and add a line "character_set_server=gbk" under this line.

Of course it is not necessary to use gbk. The editor used the national standard extension, that is, the extension gbk of gb2312, just to be cautious.

The above is the detailed content of How to solve the problem of garbled characters in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn