MySQL Chinese garbled problem perfect solution

Source: Internet
Author: User

MySQL will appear in Chinese garbled reason is the following points:
The 1.server itself sets the problem, for example still staying in Latin1
2.table language setting problem (including character and collation)
3. The connection language setting problem for the client program (e.g. PHP)
It is highly recommended to use UTF8!!!!
UTF8 can be compatible with all characters in the world!!!!
First, avoid creating database and table in Chinese garbled and view encoding method
1: Create database ' Test '
CHARACTER SET ' UTF8 '
COLLATE ' Utf8_general_ci ';
2. Create table ' Database_user ' (
' ID ' varchar (+) not NULL default ' ',
' UserID ' varchar (+) not NULL default ' ',
) Engine=innodb DEFAULT Charset=utf8;

With these 3 settings, there is no problem at all, that is, the same encoding format is used when building the library and building the table.
But if you have built libraries and tables, you can query them in the following ways.
1. View the default encoding format:
Mysql> Show variables like "%char%";
+--------------------------+---------------+
| variable_name | Value |
+--------------------------+---------------+
| character_set_client | GBK |
| character_set_connection | GBK |
| Character_set_database | UTF8 |
| Character_set_filesystem | binary |
| Character_set_results | GBK |
| Character_set_server | UTF8 |
| Character_set_system | UTF8 |
+--------------------------+-------------+
Note: Before 2 to determine, you can set the default encoding format using the set names Utf8,set names GBK;

The effect of performing set NAMES UTF8 is equivalent to setting the following:
SET character_set_client= ' UTF8 ';
SET character_set_connection= ' UTF8 ';
SET character_set_results= ' UTF8 ';

2. View the encoding format of the test database:
mysql> show create database test;
+------------+------------------------------------------------------------------------------------------------+
| Database | Create Database |
+------------+--------------------------------------------------------------------------------------------- ---+
| test | CREATE DATABASE ' test '/*!40100 DEFAULT CHARACTER SET GBK */| |
+------------+--------------------------------------------------------------------------------------------- ---+

3. View the encoding format of the YJDB database:
mysql> show create table yjdb;
| yjdb | CREATE TABLE ' yjdb ' (
' sn ' int (5) NOT NULL auto_increment,
' type ' varchar (TEN) NOT NULL,
' BRC ' varchar (6) is not NULL ,
' Teller ' int (6) is not NULL,
' telname ' varchar (TEN) is not NULL,
' date ' int (ten) is not NULL,
' count ' int (6) is not null,< br> ' Back ' int (ten) not NULL,
PRIMARY key (' SN '),
Unique key ' sn ' (' sn '),
Unique key ' sn_2 ' (' sn ')
) Engine=myis AM auto_increment=1826 DEFAULT CHARSET=GBK row_format=dynamic |

Second, avoid the import of data in Chinese garbled problem
1: Save the Data encoding format to Utf-8
Set the default encoding to UTF8:
set names UTF8;
Set database db_name default to UTF8:
ALTER database ' db_name ' default CHARACTER set UTF8 COLLATE utf8_general_ci;
Set table tb_name default encoding is UTF8:
ALTER table ' tb_name ' default CHARACTER set UTF8 COLLATE utf8_general_ci;
Import:
LOAD DATA LOCAL INFILE ' c:\\utf8.txt ' into TABLE yjdb;
2: Save the data encoding format to ANSI (that is, GBK or GB2312)
to set the default encoding to GBK:
set names GBK;
Set database db_name default encoding to GBK:
ALTER database ' db_name ' default CHARACTER set GBK COLLATE gbk_chinese_ci;
Set table Tb_name default encoding is GBK:
ALTER table ' tb_name ' default CHARACTER set GBK COLLATE gbk_chinese_ci;
Import:
LOAD DATA LOCAL INFILE ' c:\\gbk.txt ' into TABLE yjdb;

Note: 1. UTF8 do not import GBK,GBK do not import UTF8; The display of UTF8 is not supported under
2.dos;
Third, solve the problem of garbled Web pages
 
Set the site encoding to Utf-8, which is compatible with all characters in the world.
If the site has been working for a long time, there are a lot of old data, can not change the setting of Simplified Chinese, then it is recommended to set the page encoding GBK, GBK and GB2312 is the difference is: GBK can show more characters than GB2312, to display the simplified code of traditional characters, you can only use GBK.
1. Edit/etc/my.cnf, add Default_character_set=utf8 in [MySQL] section;
2. When writing the connection URL, add the useunicode=true&characterencoding=utf-8 parameter;
3. Add a "Set names UTF8" or "Set names GBK" command to the Web page code to tell the MySQL connection to use
UTF8 or 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.