MySQL Chinese display garbled characters

Source: Internet
Author: User

Recently, many posts have been posted about garbled Chinese characters, so I also summarized them as follows:

For more information, see Yang taotao's summary of various gibberish issues.
Http://topic.csdn.net/u/20071124/08/3b7eae69-ed1d-4a77-8895-9930bf3601af.html

Principles of the MySQL character set. Extracted from the official document. Http://dev.mysql.com/doc/refman/5.1/zh/charset.html

Different encoding formats may lead to the same character, and the encoding in different character sets will be different. The characters of the same Code in different character sets are also different. When the encoding of the string returned by your MySQL
The character set format (character set) is different from the character set currently used by your client tool Program (MySQL, PHP, query browser.
For example, if a British friend tells you long, when a Chinese elementary school student sees it, he will tell you "dragon" instead of "long"

It is recommended that you take a moment to take a look at the detailed introduction and examples of character sets.
Http://dev.mysql.com/doc/refman/5.1/zh/charset.html
(Chapter 1: character set support ).

Here is a summary.

The default character set in MySQL is set to four levels:Server-level, database-level, and table-level
. EventuallyField level
Character Set. Note that the first three are default settings, and your fields will use this character set setting instead of code. Therefore, we recommend that you use show create table table.
; Or show full fields from tablename;
To check the character set settings of the fields in the current table.

In MySQL, the character set of the connection environment is set to client, connection, and results.
Through these parameters, MySQL will know what character set is used by your client tool and what character set should be the result set. In this way, MySQL will make the necessary translation. Once these parameters are incorrect
A Conversion error occurs during String Conversion. Basically, 99% of garbled characters are caused by some.

Information to be checked after garbled characters. (If you need help from a friend on the Forum, we suggest you provide the following information:
)

1. Character Set settings for fields in the database table
.Show create table tablename

OrShow full columns from tablename

Mysql>Show create table T1;

+ ------- + ------------------------------------
| Table | CREATE TABLE
+ ------- + ------------------------------------
| T1 | create table 't1 '(
'Id' int (11) not null,
'C1' varchar (30) default null,
Primary Key ('id ')
) Engine = InnoDB default charset = GBK |
+ ------- + ------------------------------------
1 row in SET (0.00 Sec)

Mysql>Show full columns from T1;

+ ------- + ------------- + ---------------- + ------ + ----- +-
| FIELD | type | collation | null | key |
+ ------- + ------------- + ---------------- + ------ + ----- +-
| ID | int (11) | null | no | pri |
| C1 | varchar (30) | gbk_chinese_ci | Yes |
+ ------- + ------------- + ---------------- + ------ + ----- +-
2 rows in SET (0.00 Sec)


Mysql>

2. The currently connected system parameter show variables like 'Char %'

Mysql>Show variables like 'Char % ';

+ -------------------------- + ----------------
| Variable_name | value
+ -------------------------- + ----------------
| Character_set_client | GBK
| Character_set_connection | GBK
| Character_set_database | Latin1
| Character_set_filesystem | binary
| Character_set_results | GBK
| Character_set_server | Latin1
| Character_set_system | utf8
| Character_sets_dir | C:/program file
+ -------------------------- + ----------------
8 rows in SET (0.00 Sec)


Mysql>

1. Chinese. Make sure that the character set of this field in the table is Chinese compatible:

Big5 | big5 traditional Chinese
Gb2312 | gb2312 Simplified Chinese
GBK | GBK Simplified Chinese
Utf8 | UTF-8 Unicode

2. Make sure that the join parameter is consistent with the character set of this field. You can use set name 'charsetname ';

For example,Set Name 'gbk ';

This command modifies both character_set_client, character_set_connection, character_set_results
(If you use Chinese Characters in MySQL, you can add or modify this parameter in my. ini or my. CNF. After modifying the parameter file, you must restart the MySQL service)
[MySQL]
Default-character-set = GBK

3. php garbled characters,
Similarly, mysql_query ("Set Name 'gbk'"); other APIs are similar.

4. garbled characters in phpMyAdmin

Does $ cfg ['defaultcharset'] = 'utf-8' set in config. Inc. php of phpMyAdmin ';

5. In Windows, run the command line ("dos" window.

Left click on the title bar in the upper left corner of your DOS window, properties,
In the font, select "" to confirm
Set names 'gbk' in MySQL ';

6. Ado. net, In ADO
You can add charset = utf8 to the connection string. Similar commands are used to describe the character set of connection.
Server = myserveraddress; database = mydatabase; uid = myusername; Pwd = mypassword; charset = utf8;

7. SQL manager for MySQL

Use EMS to create a database,

Character Set to UTF-8

Client charset set UTF-8

Font charset is set to gb2312_charset

8. jdbcodbc bridging http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/bridge.html


//
Load the JDBC-ODBC bridge driver


Class. forname (sun. JDBC. ODBC. jdbcodbcdriver );


//
Setup the properties


Java. util. properties prop
=

New
Java. util. properties ();
Prop. Put (
"
Charset
"
,
"
Big5
"
);
Prop. Put (
"
User
"
, Username );
Prop. Put (
"
Password
"
, Password );


//
Connect to the database


Con
=
Drivermanager. getconnection (URL, Prop );

9. php 5.2 and later versions solve the garbled Problem
(Provided by ljf_ljf [Mark Liang)

$ Conn

=

Mysql_connect
(
"
192.168.1.20.
"
,

"
Root
"
,

"
123456
"
) Or

Die
(
"
Cocould not connect:
"

.

Mysql_error
());


$ Program_char

=

"
Utf8
"
;


$ Conn
.
Mysql_select_db
(
"
Test
"
);

//
$ Conn. mysql_query ('set @ character_set_results = "'. $ program_char .'"');



Mysql_set_charset (
$ Program_char
,
$ Conn
);

$ Charset

=

Mysql_client_encoding
(
$ Conn
);

Printf
(
"
Current Character Set is % S <br>
"
,

$ Charset
);

$ Result

=

Mysql_query
(
"
Select ID, task_no, pack_path from tb_workplan where id = 1
"
,
$ Conn
);

While
(
$ Row

=

Mysql_fetch_array
(
$ Result
,
Mysql_both )){

Printf
(
"
ID: % S <br> task_no: % S <br> pack_path: % S <br>
"
,

$ Row
[
"
ID
"
]
,

$ Row
[
1
]
,

$ Row
[
"
Pack_path
"
]);
}

$ Conn
.
Mysql_free_result
(
$ Result
);

$ Conn
.
Mysql_close
();

9. garbled stored procedure parameters

Create procedure T (AA char (10) charset 'gbk ')

Not complete...

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.