MySQL UTF8 garbled characters

Source: Internet
Author: User

Since MySQL supports Unicode, UTF8 is also considered for our web programs to keep pace with the times. In fact, UTF8 has been used for several years, and the program can basically run, there is no big problem, but when data is swapped, it is always uncomfortable.

[Symptom]

The webpage xxx. php uses EditPlus to save it as UTF8. MySQL sets default-character-set = utf8 in my. ini and addsCreate table 'xxx' (myname varchar (255) ENGINE = MyISAM default charset = utf8, with xxx. php executes insert, update, and select statements in Chinese. It seems that there is no problem, but it is garbled by phpMyAdmin. If you use a third-party tool software (such as SQLyog), the select statement is garbled, mysqldump is garbled. Of course, if you select the binary/varbinary/blob type during table creation, no garbled characters will be found, because the binary storage is specified, and MySQL does not have the encoding concept when saving data.

[Search for problems]

Although default-character-set = utf8 is set in my. ini, it is found that the following command is executed:

Mysql> show variables like 'character % ';

+ ---------------------------------------- + -------------------------

| Variable_name| Value

+ ---------------------------------------- + -------------------------

| Character_set_client| Latin1

| Character_set_connection | latin1

| Character_set_database | utf8

| Character_set_filesystem | binary

| Character_set_results| Latin1

| Character_set_server| Utf8

| Character_set_system| Utf8

| Character_sets_dir| D: \ mysql \ share \ charsets \

+ ---------------------------------------- + -------------------------

8 rows in set (0.00 sec)

 

Mysql> show variables like 'collation _ % ';

+ --------------------------------------- + ------------------

| Variable_name| Value

+ --------------------------------------- + ------------------

| Collation_connection| Latin1_swedish_ci

| Collation_database| Utf8_general_ci

| Collation_server| Utf8_general_ci

+ -------------------------------------- + ------------------

3 rows in set (0.00 sec)

It is found that all values in the Value column are utf8, and some are latin1, such as the client and connection. The webpage xxx. php is like this: from xxx. enter Chinese characters on the php page because xxx. php is UTF-8 encoded, so xxx. php converts the input Chinese Characters in UTF8 format and submits them to mysql using UTF8. However, the mysql client and connection are both latin1 and the table is UTF8. Therefore, when mysql is stored, set xxx first. the Chinese characters submitted by php are converted to latin1 format, and then to UTF8 character format. If we use a third-party software or phpMyAdmin to select this table, and the data stored in the table is UTF8 characters After latin1, it is obtained in UTF8 format, of course it looks garbled.The solution is to make all the processesUTF8.

[Solve the problem]

1. Start with my. ini.

[Client]

Default-character-set = utf8

[Mysql]

Default-character-set = utf8

[Mysqld]

Default-character-set = utf8

Default-character-set = utf8 must be added to all the preceding three sections. Normally, only mysqld is added.

Restart mysql and run

Mysql> show variables like 'character % ';

Mysql> show variables like 'collation _ % ';

Make sure that all Value items are utf8.

2. Add utf8 when creating the table. The Collation of the table field can be added but not added. The default value is utf8_general_ci.

Create table 'tablename4 '(

'Id' int (11) not null AUTO_INCREMENT,

'Varchar1' varchar (255) default null,

'Varbinary1 'varbinary (255) default null,

Primary key ('id ')

) ENGINE = MyISAM default charset = utf8

 

3. Select utf8 encoding when saving the webpage xxx. php. It is best to add

Header ('conten-type: text/html; charset = UTF-8 ');

Execute the CRUD operation first.

Mysql_query ("set names utf8 ");

The test code xxx. php is as follows:

<? Php

Header ('conten-type: text/html; charset = UTF-8 ');

Mysql_connect ("localhost", "root", "password") or die ("cocould not connect:". mysql_error ());

Mysql_select_db ("test ");

Mysql_query ("set names utf8 ");

$ Str = "CHN Software Development Co., Ltd., JPN zookeeper and zookeeper Co., Ltd., KORToo many other usersZookeeperZookeeperZookeeper, RUS has been written into our records before they were written into our records. ". time ();

$ SQL = "insert into tablename4 (varchar1, varbinary1) values ('". $ str. "', '". $ str ."')";

Echo $ SQL. "

Mysql_query ($ SQL );

 

$ Result = mysql_query ("SELECT id, varchar1, varbinary1 FROM tablename4 ");

While ($ row = mysql_fetch_array ($ result, MYSQL_BOTH )){

Printf ("ID: % s, varchar1: % s, varbinary1: % s <br>", $ row [0], $ row ["varchar1"], $ row ["varbinary1"]);

}

 

Mysql_free_result ($ result );

?>

After this setting, whether it is to insert any utf8 characters on the php page, obtained from the php page, obtained from phpMyAdmin, and obtained from the mysql third-party client software, they are all the same Chinese characters and no garbled characters will be found. mysqldump is also a Chinese character. OK. Solve the problem.

The code page in bytes is cp936 or GBK, and cannot display all UTF8 characters. It is normal to see garbled characters in character terminals, this problem can be solved in shell terminals of Unix-like systems.

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.