Causes of garbled characters
Mysql character encoding is introduced in version 4.1 and supports multiple languages. In addition, some features have exceeded those of other database systems.
Run the following Command under MySQL Command Line Client to view the mysql character set:
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)
For more information about mysql character sets, refer to
Http://www.phpfans.net/bbs/viewt... & extra = page % 3D1
Or the official mysql
Http://dev.mysql.com/doc/refman/5.1/zh/charset.html
MySQL 4.1 Character Set Support has two aspects: Character set and Collation ). The support for character sets is refined to four levels: server, database, table, and connection ).
You can run the following two commands to view the character set and sorting method settings of the system:
Mysql> show variables like 'character _ set _ % ';
+ -------------------------- + ------------------------------------------- +
| Variable_name | Value |
+ -------------------------- + ------------------------------------------- +
| Character_set_client | latin1 |
| Character_set_connection | latin1 |
| Character_set_database | latin1 |
| Character_set_filesystem | binary |
| Character_set_results | latin1 |
| Character_set_server | latin1 |
| Character_set_system | utf8 |
| Character_sets_dir | D: \ MySQL Server 5.0 \ share \ charsets \ |
+ -------------------------- + ------------------------------------------- +
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 default values of the system. Latin1 adopts latin1_swedish_ci by default, and latin1 adopts Swedish sorting.
Why is it latin1_swedish_ci by default? It is easy to trace the mysql history.
In 1979, a Swedish company Tcx wanted to develop a fast multi-thread, multi-user database system. At first, Tcx wanted to use mSQL and their own low-level fast routine (Indexed Sequential Access Method, ISAM) to connect to the database table. However, after some tests, it came to the conclusion: mSQL is not fast and flexible enough for its needs. This generates a new SQL interface for the connector database, which uses almost the same APIs as mSQL. This API is designed to facilitate the migration of Third-Party code written by mSQL to MySQL.
I believe that if mysql is developed in China, the Chinese language is also the default encoding.
Of course, you can also modify the default Character Set of mysql.
In the mysql configuration file my. ini, find the following two sentences:
[Mysql]
Default-character-set = latin1
And
# Created and no character set is defined
Default-character-set = latin1
You can modify the following values.
We do not recommend that you change the default value.
That is to say, when mysql is started, if a default character set is not specified, this value is inherited from the configuration file;
Character_set_server is set as the default character set. When a new database is created,
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 a table is created in this database,
The default Character Set of the table is set to character_set_database, which is the default Character Set of the database;
When you set a column in a table, unless explicitly specified, the default character set in this column is the default Character Set of the table.
This problem arises, for example, a database is gbk encoded. If the character set is not specified when you access the database, it is gbk.
This value will inherit the latin1 of the system, so that mysql Chinese garbled characters will be generated.
Garbled Solution
To solve the garbled problem, you must first find out the encoding used by your database. If not specified, the default value is latin1.
The three character sets gb2312, gbk, and utf8 are used most.
So how do we specify the character set of the database? The following gbk is also used as an example.
[Create a 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 rows affected, 1 warning (0.03 sec)