Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of garbled codes in MySQL

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to solve the problem of garbled codes in MySQL". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "MySQL Chinese garbled how to solve" it!

MYSQL Classic question:

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

There are many posts on the Internet to solve the problem of Chinese garbled codes, which are generally about modifying% MYSQL_DIR%/my.ini default-character-set=gbk and so on.

I tried and it didn't work. (maybe I didn't handle it properly.)

Here's what I've been thinking about for a long time, and I don't know if it's worth advocating, but the goal has definitely been achieved:

Log in to mysql->

Enter the corresponding->

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

Get: www.2cto.com

+-+

| | 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 above happens, you need to change the database code: there are two ways, the first is done at once, and the second is only valid for the current connection, disconnect the connection, and restore the pre-change state.

Method 1: alter database database name charset utf8

Method 2: set character_set_database = utf8

Reuse command: show variables like'% char%'

+-+

| | Variable_name | Value |

+-+

| | character_set_client utf8 |

| | character_set_connection utf8 www.2cto.com |

| | 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 code is consistent, but if you insert Chinese characters into the table in the database, you will still report: ERROR 1366 (HY000).

-> set character_set_client = gbk; sets the encoding received at insertion to GBK, so that it can be inserted into Chinese

At this time, the Chinese characters can be inserted, but the query results are still garbled. You can change one more place:

-> set character_set_results = gbk; set the encoding of the output to GBK

Note: this setting is only related to the current connection, the connection is disconnected, and these settings restore the default setting state of the database, so you have to set it every time you need to!

In addition: ERROR 1366 (HY000) errors mainly appear in the console (cmd/ black window) when the SQL statement is inserted into Chinese.

Insert with-front software will not report character problems, java through JDBC connection to the database to execute executeUpdate ("insert statement") Chinese can also successfully insert data and will not report an error!

PHP to solve the garbled problem, just add mysql_query ("set names' gbk'") after the connection to OK!

For example, $conn = mysql_connect ("localhost", "root", "")

Mysql_query ("set names' gbk'")

At this point, I believe that everyone on the "MySQL Chinese garbled code how to solve" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report