Troubleshooting of garbled Characters During MySQL insertion and display

Source: Internet
Author: User

 

Some time ago, I used VC + MYSQL to do things. I found garbled characters when I inserted Chinese characters. I found a lot of information on the Internet, such as setting character sets in the connected strings. I found that the problem was not solved, later I studied the MySQL character set structure and finally solved the problem.

When the MySQL server is started without adding any parameters, as shown below (My mysql version is also shown below ):

 

 

 

In my character set configuration, character_set_database and character_set_server are utf8, which is caused by the utf8 setting in my. ini MySQL configuration file:

 

 

 

 


At this point, we can find that the written data is garbled (I am blank, but there is one line of data );


Then, modify the character set when the server is started, or modify the character set in the configuration file my. ini.

 

At this time, we can find that the data originally written is still not properly displayed:

Then, insert Chinese characters and the display is still garbled:


Through the above test, we can find that if character_set_results is set only on the client side, no matter what character set on the server side, the table that creates and inserts Chinese characters is incorrect. If character_set_client is set, the effect caused by character_set_results cannot be affected.

Set character_set_results


Set character_set_client


Then, if character_set_connection is set again, the created and inserted tables will not be affected. The newly created and inserted Chinese characters tables will be correctly displayed.

The original table does not change.


The new table and Chinese characters are inserted, and the display is normal:


We can check the table structure and their character set of each table at this time:

 

 


To sum up, if garbled characters are inserted or displayed, most of them are caused by a problem with your database character set. To display Chinese characters correctly, you must set both character_set_client and
The character_set_connection parameter must be consistent with the character set specified on the server to be correctly inserted and displayed.

If you only want to display tables with correct characters inserted in the previous character set, you can only specify character_set_results on the client
That's enough.

With MySQL that correctly displays Chinese Characters

Character Set Configuration for reference:
[Client] <br/> default-character-set = GBK <br/> Port = 3306 <br/> [server] <br/> default-character-set = GBK <br/> Port = 3306 <br/> [MySQL] <br/> default-character-set = GBK <br/> [mysqld] <br/> Port = 3306 <br /> basedir = C: /program files/MySQL/<br/> datadir = C: /program files/MySQL/data/<br/> default-character-set = GBK <br/> default-storage-engine = InnoDB

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.