Sample Code for configuring MySql Encoding

Source: Internet
Author: User

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.

 
 
  1. mysql> SHOW VARIABLES LIKE 'character_set_%';   
  2. +--------------------------+----------------------------+   
  3. | Variable_name | Value |   
  4. +--------------------------+----------------------------+   
  5. | character_set_client | latin1 |   
  6. | character_set_connection | latin1 |   
  7. | character_set_database | latin1 |   
  8. | character_set_results | latin1 |   
  9. | character_set_server | latin1 |   
  10. | character_set_system | utf8 |   
  11. | character_sets_dir | /usr/share/mysql/charsets/ |   
  12. +--------------------------+----------------------------+   
  13. 7 rows in set (0.00 sec)   
  14.  
  15. mysql> SHOW VARIABLES LIKE 'collation_%';   
  16. +----------------------+-------------------+   
  17. | Variable_name | Value |   
  18. +----------------------+-------------------+   
  19. | collation_connection | latin1_swedish_ci |   
  20. | collation_database | latin1_swedish_ci |   
  21. | collation_server | latin1_swedish_ci |   
  22. +----------------------+-------------------+   
  23. 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:

 
 
  1. SET character_set_client = utf8;   
  2. SET character_set_results = utf8;   
  3. SET character_set_connection = utf8;  

2. Create a database

 
 
  1. mysql> create database name character set utf8;  

3. Create a table

 
 
  1. CREATE TABLE `type` (   
  2. `id` int(10) unsigned NOT NULL auto_increment,   
  3. `flag_deleted` enum('Y','N') character set utf8 NOT NULL default 'N',   
  4. `flag_type` int(5) NOT NULL default '0',   
  5. `type_name` varchar(50) character set utf8 NOT NULL default '',   
  6. PRIMARY KEY (`id`)   
  7. ) DEFAULT CHARSET=utf8;  

4. Modify the database to utf8.

 
 
  1. mysql> alter database name character set utf8;  

5. Use utf8to modify the table by default.

 
 
  1. mysql> alter table type character set utf8;  

6. Use utf8 to modify Fields

 

 
 
  1. 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.

Related Article

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.