Solutions to MySQL Chinese garbled characters

Source: Internet
Author: User

Typical MYSQL problems:
(ERROR 1366 (HY000): Incorrect string value: '\ xD5 \ xD4' for column 'name' at row 1)

There are a lot of posts on the Internet to solve Chinese Garbled text, basically speaking about modifying % MYSQL_DIR %/my. ini default-character-set = gbk or something.
I tried it and it didn't work. (maybe I didn't handle it)

The following is the result I have been pondering for a long time. I don't know if it is worth advocating, but the goal is absolutely achieved:

Log onto mysql --->

Go to the corresponding database --->

Enter the command: show variables like '% char % ';

Get:
+ -------------------------- +
| Variable_name | Value |
+ -------------------------- +
| Character_set_client utf8
| Character_set_connection utf8
| Character_set_database latin1
| Character_set_filesystem binary
| Character_set_results utf8
| Character_set_server utf8
| Character_set_system utf8
| Character_sets_dir D: \ MySQL \ share \ charsets \
+ -------------------------- +

If the preceding problem occurs, you need to change the database encoding. The first method is one operation, and the second method is only valid for the current connection. Disconnect and restore the status before the change.

Method 1: alter database name charset utf8;
Method 2: set character_set_database = utf8;

Run the show variables like '% char %' command again ';

+ -------------------------- +
| Variable_name | Value |
+ -------------------------- +
| Character_set_client utf8
| Character_set_connection utf8
| Character_set_database utf8
| Character_set_filesystem binary
| Character_set_results utf8
| Character_set_server utf8
| Character_set_system utf8
| Character_sets_dir D: \ MySQL \ share \ charsets \
+ -------------------------- +

At this time, the encoding is consistent. However, if you insert Chinese characters to the table in the database, ERROR 1366 (HY000) is still reported)

---> Set character_set_client = gbk; set the encoding received during insertion to GBK so that Chinese characters can be inserted.

At this time, Chinese characters can be inserted, but the query results are still garbled, and you can change one more field:

---> Set character_set_results = gbk; encode the output result as GBK.

Note: These settings are only related to the current connection and are disconnected. These settings restore the default database settings. Therefore, you must set them for each connection if needed!

In addition, an ERROR 1366 (HY000) occurs when an SQL statement is inserted into Chinese characters in the cmd/Black window of the console,
When you use mysql-front software to insert data, no character issue is reported. If you use java to connect to the database through JDBC and execute executeUpdate ("insert statement"), data can be inserted successfully in Chinese and no error is reported!

PHP solves the garbled problem by adding mysql_query ("set names 'gbk'") after connection!
For example: $ conn = mysql_connect ("localhost", "root ","");
Mysql_query ("set names 'gbk '");

Related Article

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.