MySQL database encoding problem 3 (modify the database, table, and field encoding to utf8)

Source: Internet
Author: User
Tags mysql manual

Introduction: This is a detailed page of MySQL database encoding problem 3 (modifying the database, table, and field encoding to utf8). It introduces the related knowledge, skills, and experience of PHP, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 361151 'rolling = 'no'>

Personal suggestion: use the database character set whenever possible
Utf8 (the HTML page corresponds to UTF-8), so that your data can be smoothly migrated, because utf8 is currently the most suitable character set for conversion between multiple character sets
You may not be able to correctly view the content in the database on the command line tool. I strongly recommend that you use utf8 as the default character set.
The following is a complete example:
1. Create a database table
Mysql> create database if not exists my_db default charset utf8 collate utf8_general_ci;
# Note the following sentence "collate utf8_general_ci", which roughly means sorting by utf8 validation set during sorting
# The default character set of all data tables created in this database will be utf8.

Mysql> Create Table my_table (name varchar (20) not null default '') type = MyISAM default charset utf8;
# This statement creates a table. The default character set is utf8.

2. Write Data
Example 1: insert data directly using PHP:
A. php
<? PHP
Mysql_connect ('localhost', 'user', 'Password ');
Mysql_select_db ('My _ db ');

// Note that this step is critical. Without this step, all data reads and writes Will be incorrect.
// It sets the default character set for data transmission during the database connection.
// OthersProgramming Language/Interfaces are similar, such as. Net/C #/ODBC
// For JDBC, set the connection string to "JDBC: mysql: // localhost/DB? User = user & Password = 123456 & useunicode = true & characterencoding = UTF-8"
Mysql_query ("set names utf8 ;");

// You must convert gb2312 (locally encoded) to UTF-8. You can also use the iconv () function.
Mysql_query (mb_convet_encoding ("insert into my_table values ('test');", "UTF-8", "gb2312 "));
?>

The example is to submit the insert data through the page 2:
B. php
<? PHP
// Output this page encoded as UTF-8
Header ("Content-Type: text/html; charset = UTF-8 ");

Mysql_connect ('localhost', 'user', 'Password ');
Mysql_select_db ('My _ db ');

If (isset ($ _ request ['name '))
{
// Because the character set on this page has been specified as UTF-8, no conversion encoding is required.
Mysql_query (sprintf ("insert into my_table values ('% s');", $ _ request ['name']);
}

$ Q = mysql_query ("select * From my_table ");
While ($ r = mysql_fetch_row ($ q ))
{
Print_r ($ R );
}
?>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Form action = "" method = "Post">
<Input type = "text" name = "name" value = ">
<Input type = "Submit" value = 'submit '>
</Form>

Since then, the complete example of using the utf8 character set has ended.
If you want to use gb2312 encoding, we recommend that you use Latin1 as the default Character Set of the data table so that you can insert data directly in the command line tool in Chinese and display it directly. do not use the gb2312 or GBK character sets. If you are worried about query sorting and other issues, you can use the Binary Attribute constraints, such:
Create Table my_table (name varchar (20) binary not null default '') type = MyISAM default charset Latin1;

Appendix 1: Methods for upgrading old data
Take the original character set Latin1 as an example to upgrade it to the utf8 character set. Original table: old_table (default charset = Latin1), new table: new_table (default charset = utf8 ).
Step 1: Export old data
Mysqldump -- default-character-set = Latin1-hlocalhost-uroot-B my_db -- tables old_table> old. SQL
Step 2: Convert the encoding (similar to the Unix/Linux environment)
Iconv-T UTF-8-F gb2312-c old. SQL> New. SQL
You can also remove the-F parameter to enable iconv to automatically determine the original character set.
Iconv-T UTF-8-c old. SQL> New. SQL
Here, we assume that the original data is gb2312 by default.
Step 3: Import
Modify the old. SQL statement and add an SQL statement "set names utf8;" before the insert/update statement starts. Save the statement.
Mysql-hlocalhost-uroot my_db <New. SQL
Success !!

Appendix 2: MySQL clients that support viewing utf8 character sets have
1.) mysql-front, it is said that this project has been ordered by MySQL AB to stop. For some reason, if there are many cracked versions in China, you can download it (it does not mean I recommend using the cracked version:-P ).
2.) navicat, another very good MYSQL client, just came out of the Chinese version and invited me to try it out. In general, it is still good, but it also needs to be paid.
3.) phpMyAdmin, an open-source PHP project, is very good.
4) Linux terminal tool (Linux terminal), set the character set of the terminal to utf8, connect to MySQL, execute set names utf8; can also read and write utf8 data.

Appendix 3: Convert character sets directly using the alter syntax provided by MySQL
This is a great news for many users who want to convert them to utf8. I also found this only when I was learning the MySQL manual. The usage is as follows:
Alter table old_table convert to Character Set charset_name [collate collation_name];
Before conversion, remember to back up the old table first, just in case. The following is an actual example:
Alter table 't_ yejr' convert to Character Set utf8;
This method should have been provided since MySQL 4.1. You can check whether your version is supported or not. If not, you have to follow the conversion mentioned above. Enjoy it !!!

TheArticleReproduced from the network base camp: http://www.xrss.cn/Dev/PHP/20071258977.Html

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/361151.html pageno: 1.

Personal suggestion: use the database character set whenever possible
Utf8 (the HTML page corresponds to UTF-8), so that your data can be smoothly migrated, because utf8 is currently the most suitable character set for conversion between multiple character sets
You may not be able to correctly view the content in the database on the command line tool. I strongly recommend that you use utf8 as the default character set.
The following is a complete example:
1. Create a database table
Mysql> create database if not exists my_db default charset utf8 collate utf8_general_ci;
# Note the following sentence "collate utf8_general_ci", which roughly means sorting by utf8 validation set during sorting
# The default character set of all data tables created in this database will be utf8.

Mysql> Create Table my_table (name varchar (20) not null default '') type = MyISAM default charset utf8;
# This statement creates a table. The default character set is utf8.

2. Write Data
Example 1: insert data directly using PHP:
A. php
<? PHP
Mysql_connect ('localhost', 'user', 'Password ');
Mysql_select_db ('My _ db ');

// Note that this step is critical. Without this step, all data reads and writes Will be incorrect.
// It sets the default character set for data transmission during the database connection.
// Other programming languages/interfaces are similar, such as. Net/C #/ODBC
// For JDBC, set the connection string to "JDBC: mysql: // localhost/DB? User = user & Password = 123456 & useunicode = true & characterencoding = UTF-8"
Mysql_query ("set names utf8 ;");

// You must convert gb2312 (locally encoded) to UTF-8. You can also use the iconv () function.
Mysql_query (mb_convet_encoding ("insert into my_table values ('test');", "UTF-8", "gb2312 "));
?>

The example is to submit the insert data through the page 2:
B. php
<? PHP
// Output this page encoded as UTF-8
Header ("Content-Type: text/html; charset = UTF-8 ");

Mysql_connect ('localhost', 'user', 'Password ');
Mysql_select_db ('My _ db ');

If (isset ($ _ request ['name '))
{
// Because the character set on this page has been specified as UTF-8, no conversion encoding is required.
Mysql_query (sprintf ("insert into my_table values ('% s');", $ _ request ['name']);
}

$ Q = mysql_query ("select * From my_table ");
While ($ r = mysql_fetch_row ($ q ))
{
Print_r ($ R );
}
?>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Form action = "" method = "Post">
<Input type = "text" name = "name" value = ">
<Input type = "Submit" value = 'submit '>
</Form>

Since then, the complete example of using the utf8 character set has ended.
If you want to use gb2312 encoding, we recommend that you use Latin1 as the default Character Set of the data table so that you can insert data directly in the command line tool in Chinese and display it directly. do not use the gb2312 or GBK character sets. If you are worried about query sorting and other issues, you can use the Binary Attribute constraints, such:
Create Table my_table (name varchar (20) binary not null default '') type = MyISAM default charset Latin1;

Appendix 1: Methods for upgrading old data
Take the original character set Latin1 as an example to upgrade it to the utf8 character set. Original table: old_table (default charset = Latin1), new table: new_table (default charset = utf8 ).
Step 1: Export old data
Mysqldump -- default-character-set = Latin1-hlocalhost-uroot-B my_db -- tables old_table> old. SQL
Step 2: Convert the encoding (similar to the Unix/Linux environment)
Iconv-T UTF-8-F gb2312-c old. SQL> New. SQL
You can also remove the-F parameter to enable iconv to automatically determine the original character set.
Iconv-T UTF-8-c old. SQL> New. SQL
Here, we assume that the original data is gb2312 by default.
Step 3: Import
Modify the old. SQL statement and add an SQL statement "set names utf8;" before the insert/update statement starts. Save the statement.
Mysql-hlocalhost-uroot my_db <New. SQL
Success !!

Appendix 2: MySQL clients that support viewing utf8 character sets have
1.) mysql-front, it is said that this project has been ordered by MySQL AB to stop. For some reason, if there are many cracked versions in China, you can download it (it does not mean I recommend using the cracked version:-P ).
2.) navicat, another very good MYSQL client, just came out of the Chinese version and invited me to try it out. In general, it is still good, but it also needs to be paid.
3.) phpMyAdmin, an open-source PHP project, is very good.
4) Linux terminal tool (Linux terminal), set the character set of the terminal to utf8, connect to MySQL, execute set names utf8; can also read and write utf8 data.

Appendix 3: Convert character sets directly using the alter syntax provided by MySQL
This is a great news for many users who want to convert them to utf8. I also found this only when I was learning the MySQL manual. The usage is as follows:
Alter table old_table convert to Character Set charset_name [collate collation_name];
Before conversion, remember to back up the old table first, just in case. The following is an actual example:
Alter table 't_ yejr' convert to Character Set utf8;
This method should have been provided since MySQL 4.1. You can check whether your version is supported or not. If not, you have to follow the conversion mentioned above. Enjoy it !!!

This article Reprinted from the network base camp: http://www.xrss.cn/Dev/PHP/20071258977.Html

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.