Mysql database garbled and encoding screening

Source: Internet
Author: User
Tags mysql code
Welcome to the Linux community forum and interact with 2 million technical staff. Recently, you have encountered database coding problems, which makes your system feel as uncomfortable as a beautiful girl but suddenly finds her illiterate, in fact, there are many coding problems, and there are many designs for encoding in mysql (especially). Here we will make a screening: 1mysql encoding is used below

Welcome to the Linux community forum and interact with 2 million technical staff> recently, we have encountered database coding problems, it makes your system feel as uncomfortable as a beautiful girl suddenly finds that she is not literate. In fact, many times it is a coding problem, and there are many places for mysql (especially) to design encoding, here is a screening: 1 mysql code is used below

Welcome to the Linux community forum and interact with 2 million technicians>

Recently, you have encountered database Encoding Problems, which make your system look like a beautiful girl but suddenly finds it as hard to read. In fact, many times it is a coding problem, while mysql (especially) there are a lot of places to design the code. Here is a screening:

1 mysql code

Run the following command to view the current default encodings of mysql:

Showvariables like 'character \ _ set \ _ % ';

The default mysql encoding is displayed;

Showvariables like 'collation _ % ';

Set the corresponding encoding in my. cnf:

[Client]

Default-character-set = utf8

[Mysqld]

Collation_server = utf8_general_ci

Character_set_server = utf8

[Mysql]

Default-character-set = utf8

[Mysqldump]

Default-character-set = utf8

2 database code:

Create database if not exists my_db default charset utf8COLLATE utf8_general_ci;

3. Data Table Encoding

/*! 40101 SET @ saved_cs_client

= @ Character_set_client */;

/*! 40101 SET character_set_client = utf8 */;

CREATETABLE 'access '(

'Id' int (8) unsigned not null AUTO_INCREMENT,

'Role _ id' smallint (6) unsigned not null,

'Node _ id' smallint (6) unsigned not null,

Primary key ('id '),

KEY 'nodeid' ('node _ id ')

) ENGINE = MyISAM AUTO_INCREMENT = 364 DEFAULTCHARSET = utf8;

/*! 40101 SET character_set_client = @ saved_cs_client */;

4. During the transfer

Sometimes you need to store the database, but if the database is transferred to the new database, it will be garbled. You may need:

# Mysqldump -- default-character-set utf8-u root-pmypass mydb> mydb. SQL

5. Query

Sometimes, although the SQL file encoding of mysql, database, data table, and exported data is normal, garbled characters still occur during query, probably because encoding is also required during query, function names are similar. in php + mysql, they are:

Mysql_query ('set names utf8 ');

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.