Home  >  Article  >  Database  >  How to solve MySQL Chinese garbled code?

How to solve MySQL Chinese garbled code?

黄舟
黄舟Original
2017-08-02 16:23:562058browse

Mysql database is becoming more and more popular.

When Chinese people store data in databases, Chinese characters are generally not missing. However, when MySQL saves Chinese characters, it will be garbled by default.

Here we need to make some configurations so that it is no longer "when it is saved, it will be in Chinese, and when it is found, it will be all kinds of question marks".

1. We can first check our mysql database to see if the Chinese characters are garbled.

Log in to the database, create a table in the test database, then insert Chinese and query the results. We can see that the Chinese has become a question mark.

show databases;
use test;
create table users(name varchar(50));
insert into users values('张三');
select * from users;

How to solve MySQL Chinese garbled code?

2. Let’s check the value of the variables related to each character.

Enter SHOW VARIABLES LIKE 'character%';

We can see that the value of character_set_database is latin1. Everyone's values ​​may not be the same as mine. Generally speaking, they should not all be utf8.

How to solve MySQL Chinese garbled code?

3. If there are many places that are not utf8, we need to modify the mysql configuration file my.ini.

is generally located in the root directory of the installation path, or under bin in the root directory of the installation path, or /etc/my.cnf under Linux.

How to solve MySQL Chinese garbled code?

4. In my.ini:

Search for [ client], modify or add default-character-set=utf8;

Search for [mysqld], modify or add character-set-server=utf8;

Found [mysql], modify or add default-character-set=utf8;

How to solve MySQL Chinese garbled code?


5. Restart the mysql database.

Under Windows, you can run services.msc, find the mysql service, and restart;

Under Linux, you can /etc/init.d/mysqld restart;

Mine is xampp and there is no service added. Just click stop first and then start.

How to solve MySQL Chinese garbled code?


##6. View the values ​​of each variable;

SHOW VARIABLES LIKE 'character%';

or

status

How to solve MySQL Chinese garbled code?

## 7. Test whether the code is still garbled after modification.

create database baidujingyan;
use baidujingyan;
create table users(name varchar(50));
insert into users values('张三');
select * from users;
As you can see, our database can already display Chinese normally.

How to solve MySQL Chinese garbled code?

##Note

If in the program, garbled characters will still appear , you can add set names utf8

The above is the detailed content of How to solve MySQL Chinese garbled code?. 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