Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the MySQL character set?

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

What is the MySQL character set, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

About MySQL character set [@ more@] first of all, this article is purely my personal experience, applicable to my common environment and projects.

Personally, it is recommended that the database character set as far as possible to use utf8 (utf-8), so that your data can be transferred smoothly, because the utf8 character set is the most suitable for the conversion between different character sets, although you can not correctly view the contents of the database on the command line tool, I still strongly recommend using utf8 as the default character set.

Here is a complete example:

1. Create a database table

Mysql > CREATE DATABASE IF NOT EXISTS my_db default charset utf8 COLLATE utf8_general_ci

# notice the following sentence "COLLATE utf8_general_ci", which roughly means to sort according to the utf8 variable format when sorting.

# then the default character set of all data tables created under this database will be utf8

Mysql > create table my_table (name varchar (20) not null default'') type=myisam default charset utf8

# this sentence is to create a table and set the default character set to utf8

two。 Write data

Example 1 is to insert data directly through php:

A.php

Mysql_connect ('localhost','user','password')

Mysql_select_db ('my_db')

/ / Please note that this step is critical. Without this step, all data reads and writes will be incorrect.

/ / its function is to set the default character set for data transfer during this database connection.

Mysql_query ("set names utf8;")

/ / you must convert gb2312 (local encoding) to utf-8, or you can use the iconv () function

Mysql_query (mb_convet_encoding ("insert into my_table values ('test');", "utf-8", "gb2312"))

? >

An example is to submit insert data through the page 2:

B.php

/ / output this page is 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'))

{

/ / since the character set of this page has been specified as utf-8 above, there is no need to convert the encoding

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)

}

? >

Since then, the complete example of using the utf8 character set has come to an end.

If you want to use gb2312 encoding, it is recommended that you use latin1 as the default character set of the datasheet, so that you can insert data in Chinese directly into the command line tool and display it directly. Instead of using character sets such as gb2312 or gbk, if you are worried about query sorting, you can use binary attribute constraints, such as:

Create table my_table (name varchar (20) binary not null default'') type=myisam default charset latin1

Attached: methods for upgrading old data

Take the original character set as latin1 as an example, upgrade to utf8 character set. The original table: old_table (default charset=latin1), the 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: transcoding

Iconv-t utf-8-f gb2312-c old.sql > new.sql

Here, it is assumed that the original data is gb2312 encoded by default.

Step 3: import

Modify old.sql, add a sql statement: "SET NAMES utf8;", save.

Mysql-hlocalhost-uroot my_db < new.sql

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report