The following articles mainly describe the actual operation steps of MySql encoding settings. We all know that the Character Set Support of MySQL 4.1 has two main aspects, character set and its actual sorting method (Collation ).
The support for character sets in practical applications is refined to four levels:
Server, database, table, and connection ).
You can use the following two commands or mysql> status 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_results | latin1 |
- | character_set_server | latin1 |
- | character_set_system | utf8 |
- | character_sets_dir | /usr/share/mysql/charsets/ |
- +--------------------------+----------------------------+
- 7 rows in set (0.00 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.00 sec)
The values listed above are the default values of the system. (It's strange how latin1's Swedish sorting method is used by default )...
When we access the MySQL database through PHP in the original way, even if the default Character Set of the table is set to utf8 and the query is sent through the UTF-8 encoding, you will find that the database is still garbled. The problem lies in the connection layer. The MySql encoding setting solution is to execute the following statement before sending the query:
1. set names 'utf8 ';
It is equivalent to the following three commands:
- SET character_set_client = utf8;
- SET character_set_results = utf8;
- SET character_set_connection = utf8;
2. Create a database
- mysql> create database name character set utf8;
3. Create a table
- CREATE TABLE `type` (
- `id` int(10) unsigned NOT NULL auto_increment,
- `flag_deleted` enum('Y','N') character set utf8 NOT NULL default 'N',
- `flag_type` int(5) NOT NULL default '0',
- `type_name` varchar(50) character set utf8 NOT NULL default '',
- PRIMARY KEY (`id`)
- ) DEFAULT CHARSET=utf8;
4. Modify the database to utf8.
- mysql> alter database name character set utf8;
5. Use utf8to modify the table by default.
- mysql> alter table type character set utf8;
6. Use utf8 to modify Fields
- mysql> alter table type modify type_name varchar(50) CHARACTER SET utf8;
The above content is an introduction to MySql encoding settings. I hope you will find some gains.