Home  >  Article  >  Database  >  Analysis and solution to the Chinese garbled problem of MySQL data

Analysis and solution to the Chinese garbled problem of MySQL data

PHPz
PHPzOriginal
2023-04-17 16:39:193438browse

In daily database applications, we often encounter the problem of MySQL Chinese garbled characters. This problem generally seems to be because the default character set setting of the MySQL database is incorrect, resulting in the inability to display Chinese characters correctly when inserting or displaying them. Let's analyze the causes of MySQL Chinese garbled characters and how to solve them.

1. Problem

MySQL Chinese garbled characters generally appear as follows:

1. When operating the MySQL database, if there are Chinese characters in the inserted data, the Chinese characters will Become gibberish.

2. When data is retrieved from the database, Chinese characters may also be garbled.

If the above situation occurs, it is obvious that the character set of MySQL is set incorrectly.

2. Cause of the problem

The MySQL Chinese character set is not set correctly when creating the table, resulting in garbled characters in both insertion and query characters.

Then we can set it up through some simple methods.

3. Solution

1. Modify the MySQL default character set

The method to modify the MySQL default character set is relatively simple:

1) Find the MySQL Configuration file my.ini or my.cnf file

Windows

C:\Program Files\MySQL\MySQL Server 5.6\my.ini

Linux

/etc/my.cnf

2) Modify all character sets in the configuration file to utf8 as follows:

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
default-storage-engine = INNODB
character-set-server = utf8
collation- server = utf8_general_ci

3) Restart the MySQL service

2. Modify the character set of the database and table

You can specify the character set when creating the database and table, or you can After creation, modify it through the alter statement:

1) Specify the character set when creating the database

CREATE DATABASE db_name CHARACTER SET utf8 COLLATE utf8_general_ci;

2) Specify the character set when creating the table Set

CREATE TABLE t1 (

id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) CHARACTER SET utf8 COLLATE utf8_general_ci

);

3) Modify the character set of the table

ALTER TABLE t1 CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

4. Summary

The MySQL Chinese garbled problem is a relatively common problem, but it is actually relatively easy to solve it after finding the cause. As long as the character set of MySQL is correctly set, the problem of garbled Chinese characters can be avoided. Hope this article is helpful to everyone.

The above is the detailed content of Analysis and solution to the Chinese garbled problem of MySQL data. 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