MySQL Character set encoding garbled test is as follows

Source: Internet
Author: User
Tags locale

Create three table TB_LATIN1,TB_UTF8,TB_GBK, encoded as LATIN1/UTF8/GBK

The "Hello a" string is encoded as follows
GBK:%c4%e3%ba%c3%61
UTF-8:%e4%bd%a0%e5%a5%bd%61

The test code is as follows

<?PHP//fileencoding=gb2312mysql_query("Set Names GBK");mysql_query("INSERT into test.tb_latin values (' GBK ', ' Hello A ')");/*The connected character set is GBK, a character is represented by one to two bytes, the character set passed in%c4%e3%ba%c3%61 stored is latin1, a character is represented by a byte that MySQL converts the incoming three characters to latin1 three characters into%3f (not found to the corresponding conversion table)%3f%61 causes a data store error (?? A) (storage occupies 3 bytes)*/mysql_query("INSERT into Test.tb_utf8 values (' GBK ', ' Hello A ')");/*The connected character set is GBK, a character is represented by one to two bytes, the character set passed in%c4%e3%ba%c3%61 stored is utf8, and a character from one to four bytes means that MySQL converts the three characters passed in to utf8 three characters into%e4%bd%a 0%e5%a5%bd%61 (storage occupies 7 bytes) read when the connection is set to UTF-8 and GB2312 can be, but the specified as Latin1 will appear the transcoding failure return error content (?? A) UTF-8 returns 7 bytes, GB2312 returns 5 bytes*/mysql_query("INSERT into TEST.TB_GBK values (' GBK ', ' Hello A ')");/*The connected character set is GBK, a character is represented by one to two bytes, the character set passed in%c4%e3%ba%c3%61 stored is GBK, save as%c4%e3%ba%c3%61 storage consumes 5 bytes read when the connection is set to UTF-8 and GB2312 are Yes, but a transcoding failure that is specified as Latin1 returns the error content (?? A) UTF-8 returns 7 bytes, GB2312 returns 5 bytes*/? ><?PHP//fileencoding=gb2312mysql_query("Set Names Latin1");mysql_query("INSERT into test.tb_latin values (' gblatin1 ', ' Hello A ')");/*The connected character set is Latin1, a character is represented by a byte, the character set passed in%c4%e3%ba%c3%61 stored is latin1, a character is represented by one to four bytes, stored as%c4%e3%ba%c3%61 (Storage occupies 5 bytes) Read-time connection encoded as latin1, the front end gets five characters (%c4%e3%ba%c3%61) If the VIM (term) environment is exactly the GB2312 encoding environment, the text "Hello a" is displayed if the Vim (term) display environment is encoded as LAT in1, display (garbled) if the vim (term) locale is UTF8, transcoding (from Latin1 to UTF8) fails to display (??? A) When the connection is read Gbk,mysql cannot convert five characters to 5 corresponding GBK encoding, return (???? A) error content reads when the connection is utf8,mysql converts five characters to%c3%84%c3%a3%c2%ba%c3%83%61 error content, specific rendering see term encoding*/mysql_query("INSERT into Test.tb_utf8 values (' gblatin1 ', ' Hello A ')");/*The connected character set is Latin1, a character is represented by a byte, the character set passed in%c4%e3%ba%c3%61 is UTF8, a character is represented by a byte, and stored as%c3%84%c3%a3%c2%ba%c3%83%61 (save Storage occupies 5 bytes) When reading the connection code is latin1, the front end gets five characters (%c4%e3%ba%c3%61) (MySQL converts the wrong encoding to latin1) if the VIM (term) environment is exactly the GB2312 encoding environment, the text "Hello A If the VIM (term) display environment is encoded as latin1, display (garbled) if the vim (term) locale is UTF8, transcoding (from Latin1 to UTF8) fails to display (??? A) When the connection is read Gbk,mysql cannot convert five characters to 5 corresponding GBK encoding, return (???? A) error content reads when the connection is utf8,mysql converts five characters to%c3%84%c3%a3%c2%ba%c3%83%61 error content, specific rendering see term encoding*/mysql_query("INSERT into TEST.TB_GBK values (' gblatin1 ', ' Hello A ')");/*The connected character set is Latin1, a character is represented by a byte, the character set passed in%c4%e3%ba%c3%61 is GBK, a character is represented by one to two bytes, and MySQL cannot convert five characters to 5 corresponding GBK encoding, storage (???? A) Error content*/? ><?PHP//Fileencoding=utf8mysql_query("Set Names UTF8");mysql_query("INSERT into test.tb_latin1 values (' UTF8 ', ' hello ')");/*The connected character set is UTF-8, a character is represented by one to four bytes, the character set passed in%e4%bd%a0%e5%a5%bd%61 stored is latin1, and a character is represented by a byte that MySQL converts the three characters passed in to latin1 three characters Into%3f (cannot find the corresponding Conversion table table)%3f%61 cause data store error (?? A) (storage occupies 3 bytes)*/mysql_query("INSERT into Test.tb_utf8 values (' UTF8 ', ' hello ')");/*The connected character set is UTF-8, a character is represented by one to four bytes, the character set passed into the%e4%bd%a0%e5%a5%bd%61 stored is UTF8, a character is represented by one to four bytes, and the%e4%bd%a0%e5%a5%bd%61 read The fetch-time connection is set to both UTF-8 and GB2312, but a transcoding failure that is specified as Latin1 will cause the error content to be returned (?? A) UTF-8 returns 7 bytes, GB2312 returns 5 bytes*/mysql_query("INSERT into TEST.TB_GBK values (' UTF8 ', ' hello ')");/*The connected character set is UTF-8, a character is represented by one to four bytes, the character set passed in%e4%bd%a0%e5%a5%bd%61 stored is GBK, save as%c4%e3%ba%c3%61 storage consumes 5 bytes read when the connection is set to UTF-8 And GB2312 can be, but the specified as Latin1 will appear transcoding failure return error content (?? A) UTF-8 returns 7 bytes, GB2312 returns 5 bytes*/? ><?PHP//Fileencoding=utf8mysql_query("Set Names Latin1");mysql_query("INSERT into test.tb_latin values (' latin1 ', ' Hello ')");/*The connected character set is Latin1, a character is represented by a byte, passed in%e4%bd%a0%e5%a5%bd%61 The stored character set is Latin1, a character is represented by a byte and stored as%e4%bd%a0%e5%a5%bd %61 (Storage occupies 7 bytes) reads when the connection code is latin1, the front end gets seven characters (%e4%bd%a0%e5%a5%bd%61) If the VIM (term) environment is exactly the UTF8 encoding environment, the text "Hello a" is displayed as Fruit Vim (term) display environment encoded as latin1, display (garbled) if the vim (term) locale is GBK, garbled reads when the connection is gbk,mysql unable to convert seven characters to the corresponding seven corresponding GBK encoding, return (?????? A) Error content read when connected to Utf8,mysql convert seven characters to%c3%a4%c2%bd%c2%a0%c3%a5%c2%a5%c2%bd%61 content returned (error displayed)*/mysql_query("INSERT into Test.tb_utf8 values (' latin1 ', ' Hello ')");/*the character set of the connection is Latin1, a character is represented by a byte, an incoming%e4%bd%a0%e5%a5%bd%61 The stored character set is UTF8, a character is represented by one to four bytes, stored as%c3%a4%c2%bd%c2%a0%c        3%a5%c2%a5%c2%bd%61 (storage occupies 13 bytes) The connection is encoded as latin1 when read, the front end gets five characters (%e4%bd%a0%e5%a5%bd%61) (MySQL converts 13 bytes of 7 characters to latin1) If the VIM (term) environment is exactly the UTF8 encoding environment, the text "Hello a" is displayed if vim (term) display environment is encoded as latin1, display (garbled) if the vim (term) locale is GBK, garbled reads are connected as Gbk,m Ysql cannot convert 7 characters to 7 corresponding GBK encodings, returning (?????? A) error content reads when the connection is utf8,mysql converts five characters to%c3%a4%c2%bd%c2%a0%c3%a5%c2%a5%c2%bd%61 error content, specific rendering see term encoding*/mysql_query("INSERT into TEST.TB_GBK values (' latin1 ', ' Hello ')");/*the character set of the connection is Latin1, a character is represented by a byte, an incoming%e4%bd%a0%e5%a5%bd%61 The stored character set is GBK, a character is represented by one to two bytes, and MySQL cannot turn 7 characters into 7 corresponding GBK encodings, stored (? ????? A) Error content*/?>

Database queries are as follows

Mysql> Select CharSet, Data,length (data) from tb_latin;+----------+---------+--------------+| CharSet | Data | Length (data) |+----------+---------+--------------+| GBK |??            A | 3 | | gblatin1 |????            A | 5 | | UTF8 |??            A | 3 | | latin1 |??????            A | 7 |+----------+---------+--------------+4 rows in Set (0.00 sec) mysql> Select CharSet, Data,length (data) from Tb_utf8; +----------+---------+--------------+| CharSet | Data | Length (data) |+----------+---------+--------------+| GBK |???            A | 7 | | gblatin1 |????            A | 9 | | UTF8 |???            A | 7 | | latin1 |??????           A | |+----------+---------+--------------+4 rows in Set (0.00 sec) mysql> Select CharSet, Data,length (data) from TB_GBK; +----------+---------+--------------+| CharSet | Data | Length (data) |+----------+---------+--------------+| GBK |???            A | 5 | | gblatin1 |????            A | 5 | | Utf8    | ???            A | 5 | | latin1 |??????            A | 7 |+----------+---------+--------------+

The following test code is attached:

<? PHP // ... mysql_query ("Set names Latin1"); $result=mysql_query("select * from TEST.TB_GBK where charset= ' latin1 '");  while ($row=mysql_fetch_row($result)) {var_dump($row ); Echo Bin2Hex ($row[1]);}? >

MySQL Character set encoding garbled test is as follows

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.