Summary of Chinese garbled solution method in MySQL Database

Source: Internet
Author: User
Tags character set mysql version php file create database

Recently also constantly improve the Wblog program, so far, Wblog has not installed the program, the two days are busy writing, to install the program has come to an end, can basically install, but is not yet detect PHP version and MySQL version of some parameters of the set work

Yes. Test process, found inserted into the database of Chinese garbled, I constantly modify, keep running, and then at least dozens of times, I put the relevant data of these processes are recorded, to be sorted out. This time I'm going to make a case. I've met before.

Database garbled related problems, but the problem solved, also do not ask why, so nothing, long, when again found and forget how to solve, and then spend a lot of time to search the Internet to find the answer, never out of this circle. Taking advantage of this rare

opportunity to make the database Chinese garbled catch!
The reasons for the garbled characters in Chinese inserted into the database may be influenced by the following three factors:
1, the data table character set "charset= character encoding"
2, PHP file encoding "<meta http-equiv=" Content-type "content=" text/html; charset= character encoding "/>"
3. Whether the mysql_query ("Set names character encoding") statement is executed before the data is stored.

setting of the data table character set

Here's the SQL data I want to test

The code is as follows Copy Code

"CREATE TABLE ' Wb_node ' (

The ' id ' smallint (6) unsigned not NULL auto_increment,

Not NULL for ' name ' varchar,

' title ' varchar Not NULL,

' Ord ' tinyint (3) not NULL DEFAULT ' 0 ',

The ' status ' tinyint (1) Not NULL DEFAULT ' 0 ',

' Remark ' varchar not NULL,

The ' sort ' smallint (6) unsigned not NULL,

The ' PID ' smallint (6) unsigned not NULL,

"Level" tinyint (1) unsigned not NULL,

One PRIMARY KEY (' id '),

KEY ' Level ' (' Level '),

KEY ' pid ' (' pid '),

KEY ' status ' (' status '),

KEY ' name ' (' name ')

Engine=myisam Charset=utf8; "

The table character set in the above datasheet is "Charset=utf8", and of course it can be "CHARSET=GBK" or "charset=gb2312" in Chinese.
Code for PHP files
PHP file encoding I was generated using Adobe Dreamweaver CS5, as shown in the figure:


Executing SQL encoding statements

The code is as follows Copy Code

$link = Mysql_connect ($mysql _host, $mysql _user, $mysql _pass)) {   

02          echo "Database connection failed! Please return to the previous page to check the connection parameters;   

03    } else {  

04         if (mysql_query ("CREATE DATABASE ' $mysql _dbname ')") {  

05             mysql_query ("Set names UTF8");         

06    }else{  

07     exit (" Create DATABASE Failed ");   

08    };  

09     if (! mysql_select_db ($mysql _dbname)) {  

10     exit ("Database not Found:". $mysql _dbname. ");   

11    };

mysql_query ("Set names character encoding") that supports Chinese character encoding can be UTF8, GBK, and gb2312.
The following is the data to be inserted into the datasheet ' Wb_node '.

The code is as follows Copy Code


"INSERT into ' Wb_node ' VALUES (' 1 ', ' admin ', ' backstage items ', ' 0 ', ' 1 ', ' Backstage items ', ' 1 ', ' 0 ', ' 1 ');";

INSERT into ' Wb_node ' VALUES (' 2 ', ' News ', ' article Management ', ' 0 ', ' 1 ', ' article Management ', ' 1 ', ' 1 ', ' 2 '); ";

"INSERT into ' Wb_node ' VALUES (' 3 ', ' Index ', ' home admin ', ' 0 ', ' 1 ', ' Home module ', ' 1 ', ' 1 ', ' 2 ');";

"INSERT into ' Wb_node ' VALUES (' 4 ', ' photo ', ' picture Management ', ' 0 ', ' 1 ', ' picture Management ', ' 1 ', ' 1 ', ' 2 ');";

INSERT into ' Wb_node ' VALUES (' 5 ', ' Download ', ' download admin ', ' 0 ', ' 1 ', ' download admin ', ' 1 ', ' 1 ', ' 2 '); ";

INSERT into ' Wb_node ' VALUES (' 6 ', ' Page ', ' single page Management ', ' 0 ', ' 1 ', ' single page Management ', ' 1 ', ' 1 ', ' 2 '); ";

Modified INSERT into ' Wb_node ' VALUES (' 7 ', ' Columns ', ' column management ', ' 0 ', ' 1 ', ' column management ', ' 1 ', ' 1 ', ' 2 '); ";

"INSERT into ' Wb_node ' VALUES (' 8 ', ' databakup ', ' Data backup ', ' 0 ', ' 1 ', ' Data backup ', ' 1 ', ' 1 ', ' 2 ');";

"INSERT into ' Wb_node ' VALUES (' 9 ', ' roles ', ' role management ', ' 0 ', ' 1 ', ' role management ', ' 1 ', ' 1 ', ' 2 ');";

INSERT into ' Wb_node ' VALUES (' Ten ', ' config ', ' Configuration Management ', ' 0 ', ' 1 ', ' Configuration Management ', ' 1 ', ' 1 ', ' 3 '); ";

One "INSERT into ' Wb_node ' VALUES (' One ', ' blog ', ' blog Management ', ' 0 ', ' 1 ', ' blog admin ', ' 1 ', ' 1 ', ' 2 ');";

INSERT into ' Wb_node ' VALUES (' ', ' user ', ' admin ', ' 0 ', ' 1 ', ' user admin ', ' 1 ', ' 1 ', ' 2 '); ";

INSERT into ' Wb_node ' VALUES (' ', ' guestbook ', ' message management ', ' 0 ', ' 1 ', ' message Management ', ' 1 ', ' 1 ', ' 2 '); ";

"INSERT into ' Wb_node ' VALUES ('", ' link ', ' Friendship link Management ', ' 0 ', ' 1 ', ' Friendship link Management ', ' 1 ', ' 1 ', ' 2 '); ";

INSERT into ' Wb_node ' VALUES (' ', ' comment ', ' comment management ', ' 0 ', ' 1 ', ' Comment management ', ' 1 ', ' 1 ', ' 2 '); ";

INSERT into ' Wb_node ' VALUES (' ', ' upload ', ' attachment Management ', ' 0 ', ' 1 ', ' Attachment Management ', ' 1 ', ' 1 ', ' 2 '); ";

"INSERT into ' Wb_node ' VALUES (' ', ' announce ', ' Bulletin Management ', ' 0 ', ' 1 ', ' Bulletin Management ', ' 1 ', ' 1 ', ' 2 ');";

INSERT into ' Wb_node ' VALUES (' ', ' banner ', ' Advertising management ', ' 0 ', ' 1 ', ' advertising management ', ' 1 ', ' 1 ', ' 2 '); ";

INSERT into ' Wb_node ' VALUES (' ', ' index ', ' List of Friends ', ' 1 ', ' 1 ', ' Friendship list ', ' 1 ', ' 14 ', ' 3 '); ";

Well, the preparation for the test has been done. To continue ...

First, keep the following conditions 1, 2 unchanged, only change the condition (3) of the Code


1. Data table Character set: Charset=utf8


2. php File code: gb2312


3, mysql_query ("Set names character encoding")


3.1, mysql_query ("Set names GBK"); display garbled figure (1)

figure (1)

3.2, mysql_query ("Set names gb2312"); display garbled figure (1)


3.3, mysql_query ("Set names UTF8"), showing normal as shown (2):

figure (2):


3.4 Do not execute mysql_query ("Set names character encoding") statement, display garbled figure (3)

figure (3):
From the first round of test results can be known, the data table's character set to Charset=utf8, and the use of the PHP file encoding for gb2312, at this time in order to avoid Chinese garbled, remember to execute the mysql_query ("Set names UTF8") statement before storing the data.


Second, keep the following conditions 1, 2 unchanged, only change the condition (3) of the Code


1. Data table Character set: Charset=utf8


2. php File code: Utf-8


3, mysql_query ("Set names character encoding")


3.1, mysql_query ("Set names GBK"); display garbled figure (1)

3.2, mysql_query ("Set names gb2312"); display garbled figure (4)

Figure (4):

3.3, mysql_query ("Set names UTF8"); display normal as shown (2)

3.4 Do not execute mysql_query ("Set names character encoding") statement, display garbled map (3)


the second-round test differs from the first round by changing the encoding of the PHP file to Utf-8 and the test results are the same as before. It seems that the encoding of PHP files will not affect Chinese garbled, of course, this is only preliminary results, but also continue.

Third, keep the following conditions 1, 2 unchanged, only change the condition (3) of the Code

1. Data table Character set: CHARSET=GBK


2. php File code: gb2312


3, mysql_query ("Set names character encoding")


3.1, mysql_query ("Set names GBK"); display garbled figure (1)

3.2, mysql_query ("Set names gb2312"); Display garbled map (3)


3.3, mysql_query ("Set names UTF8"), Chinese normal display as shown (2)


3.4 Do not execute mysql_query ("Set names character encoding") statement, display garbled figure (5)

Figure (5)


The third round of character set code and PHP file encoding have been changed, compared to the first two rounds, know, in the data before the implementation of the mysql_query ("Set names UTF8"), Chinese always normal display, PHP file coding seems to have no effect on the garbled.

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.