Recently, many posts have been posted about Chinese garbled characters, so I also summarized the principles of the MySQL character set. Extracted from the official document. Dev. mysql. comdoc
Recently on the Chinese display garbled stickers more, so also made a summary: can refer to Yang taotao Moderator of the various garbled problem summary, http://topic.csdn.net/u/20071124/08/3b7eae69-ed1d-4a77-8895-9930bf3601af.html MySQL character set principles. Extracted from the official document. Http://dev.mysql.com/doc
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 format (character set) of the string returned by your MySQL is associated with your client tool Program (mysql, php, query browser ,...) if the character set used is different, garbled characters are generated. 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 levelCharacter 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; or show full fields from tableName; to check the character set settings of fields in the current table.
MySQL sets the character set of the connection environment with the Client, connection, and results parameters. Through these parameters, MySQL will know what character set your Client tool uses and what character set the result set should be. In this way, MySQL will perform necessary translation. Once these parameters are incorrect, the conversion errors of strings during the conversion process will naturally occur. Basically, 99% of garbled characters are caused by some.
Information to be checked after garbled characters. (If you need help from friends on the Forum, we suggest you provide the following information)
1. Character Set settings for fields in the database table.Show create table TableNameOrShow 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 ADOYou 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
", $ 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
Task_no: % s
Pack_path: % s
", $ 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...