Home  >  Article  >  Backend Development  >  Summarize the causes and solutions of MySQL Chinese garbled code, phpmyadmin garbled code, and php garbled code. Page 1/3_PHP Tutorial

Summarize the causes and solutions of MySQL Chinese garbled code, phpmyadmin garbled code, and php garbled code. Page 1/3_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:54:43928browse

Causes of garbled characters

MySQL character encoding was introduced in version 4.1, supports multiple languages, and some features have surpassed other database systems.

我们可以在MySQL Command Line Client 下输入如下命令查看mysql的字符集

mysql> SHOW CHARACTER SET;
+----------+-----------------------------+---------------------+--------+
| Charset  | Description                 | Default collation   | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5     | Big5 Traditional Chinese    | big5_chinese_ci     | 2      |
| dec8     | DEC West European           | dec8_swedish_ci     | 1      |
| cp850    | DOS West European           | cp850_general_ci    | 1      |
| hp8      | HP West European            | hp8_english_ci      | 1      |
| koi8r    | KOI8-R Relcom Russian       | koi8r_general_ci    | 1      |
| latin1   | cp1252 West European        | latin1_swedish_ci   | 1      |
| latin2   | ISO 8859-2 Central European | latin2_general_ci   | 1      |
| swe7     | 7bit Swedish                | swe7_swedish_ci     | 1      |
| ascii    | US ASCII                    | ascii_general_ci    | 1      |
| ujis     | EUC-JP Japanese             | ujis_japanese_ci    | 3      |
| sjis     | Shift-JIS Japanese          | sjis_japanese_ci    | 2      |
| hebrew   | ISO 8859-8 Hebrew           | hebrew_general_ci   | 1      |
| tis620   | TIS620 Thai                 | tis620_thai_ci      | 1      |
| euckr    | EUC-KR Korean               | euckr_korean_ci     | 2      |
| koi8u    | KOI8-U Ukrainian            | koi8u_general_ci    | 1      |
| gb2312   | GB2312 Simplified Chinese   | gb2312_chinese_ci   | 2      |
| greek    | ISO 8859-7 Greek            | greek_general_ci    | 1      |
| cp1250   | Windows Central European    | cp1250_general_ci   | 1      |
| gbk      | GBK Simplified Chinese      | gbk_chinese_ci      | 2      |
| latin5   | ISO 8859-9 Turkish          | latin5_turkish_ci   | 1      |
| armscii8 | ARMSCII-8 Armenian          | armscii8_general_ci | 1      |
| utf8     | UTF-8 Unicode               | utf8_general_ci     | 3      |
| ucs2     | UCS-2 Unicode               | ucs2_general_ci     | 2      |
| cp866    | DOS Russian                 | cp866_general_ci    | 1      |
| keybcs2  | DOS Kamenicky Czech-Slovak  | keybcs2_general_ci  | 1      |
| macce    | Mac Central European        | macce_general_ci    | 1      |
| macroman | Mac West European           | macroman_general_ci | 1      |
| cp852    | DOS Central European        | cp852_general_ci    | 1      |
| latin7   | ISO 8859-13 Baltic          | latin7_general_ci   | 1      |
| cp1251   | Windows Cyrillic            | cp1251_general_ci   | 1      |
| cp1256   | Windows Arabic              | cp1256_general_ci   | 1      |
| cp1257   | Windows Baltic              | cp1257_general_ci   | 1      |
| binary   | Binary pseudo charset       | binary              | 1      |
| geostd8  | GEOSTD8 Georgian            | geostd8_general_ci  | 1      |
| cp932    | SJIS for Windows Japanese   | cp932_japanese_ci   | 2      |
| eucjpms  | UJIS for Windows Japanese   | eucjpms_japanese_ci | 3      |
+----------+-----------------------------+---------------------+--------+
36 rows in set (0.02 sec)

更多mysql的字符集知识可以参考本论坛的
http://www.phpfans.net/bbs/viewt ... &extra=page%3D1
或者mysql官方的
http://dev.mysql.com/doc/refman/5.1/zh/charset.html

MySQL 4.1的字符集支持(Character Set Support)有两个方面:字符集(Character set)和排序方式(Collation)。对于字符集的支持细化到四个层次: 服务器(server),数据库(database),数据表(table)和连接(connection)。
To view the system's character set and sorting settings, you can use the following two commands:

mysql> SHOW VARIABLES LIKE 'character_set_%';
+-------- ------------------+---------------------------------- ------------+
| Variable_name                                                                                  -+---------------------------------------------+
| character_set_client | latin1 | latin1 |
| character_set_connection | latin1 |
| base | latin1 | character_set_results |
| character_set_results | latin1                                                                                                                          utf8                                                                                                               | ---------------------------------------+
8 rows in set (0.06 sec )

mysql> SHOW VARIABLES LIKE 'collation_%';
+----------------------+------ -------------+
| Variable_name | Value |
+------------------------+- ------------------+
| collation_connection | latin1_swedish_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+--- ------------------+-------------------+
3 rows in set (0.02 sec )

The values ​​listed above are the system default values. The default collation rule of latin1 is latin1_swedish_ci, which is the Swedish sorting method of latin1.
Why is the default collation rule latin1_swedish_ci? It is easy to find out if you trace the history of mysql

In 1979, a Swedish company Tcx wanted to develop a Fast multi-threaded, multi-user database system. Tcx initially wanted to use mSQL and their own fast low-level routines (Indexed Sequential Access Method, ISAM) to connect to database tables. However, after some testing, they concluded that mSQL was not fast and flexible enough for its needs. This resulted in a new SQL interface to the connector database, which uses almost the same API interface as mSQL. This API is designed to make it easier to port third-party code written for mSQL to MySQL.

I believe that if mysql was developed in China, then Chinese would also be encoded by default

Of course, we can also modify the default character set of mysql ourselves
In the mysql configuration document my.ini, find The following two sentences:

[mysql]

default-character-set=latin1

and

# created and no character set is defined
default-character-set=latin1

Just modify the following value.

It is not recommended to change it here, the default value is still retained
That is to say, when starting mysql, if a default character set is not specified, this value is inherited from the configuration file;
At this time character_set_server is set to this default character set; when creating a new database,
Unless explicitly specified, the character set of this database is set to character_set_server by default; when a database is selected,
character_set_database is set to the default character set of this database; When creating a table in this database,
The default character set of the table is set to character_set_database, which is the default character set of this database;
When in When setting a column in a table, unless explicitly specified, the default character set for this column is the default character set for the table.

This problem will arise, if a database is gbk encoded. If the character set is not specified when accessing the database, it is gbk.
Then this value will inherit the latin1 of the system, thus making mysql Chinese garbled code.

Solution to garbled codes

To solve the garbled code problem, you must first figure out what encoding your database uses. If not specified, the default is latin1.
The three character sets we use most should be gb2312, gbk, and utf8.

So how do we specify the character set of the database? The following is an example of gbk

【Create database in MySQL Command Line Client】

mysql> CREATE TABLE `mysqlcode` (
-> `id` TINYINT( 255 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
-> `content` VARCHAR( 255 ) NOT NULL
-> ) TYPE = MYISAM CHARACTER SET gbk COLLATE gbk_chinese_ci;
Query OK, 0 affected rows , 1 warning (0.03 sec )

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318431.htmlTechArticleCause of garbled characters MySQL character encoding was introduced in version 4.1, supports multiple languages, and some features have surpassed others Database system. We can enter under MySQLCommandLineClient...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn