Home > Database > Mysql Tutorial > body text

Mysql数据库乱码与编码问题筛查_MySQL

WBOY
Release: 2016-06-01 13:29:39
Original
1138 people have browsed it

bitsCN.com

Mysql数据库乱码与编码问题筛查

 

最近接连遇到数据库编码问题,让你的系统本来像个美丽的姑娘却忽然发现她不识字一样难受,其实很多时候是编码的问题,而mysql(特别地)设计编码的地方很多,在这里做一个筛查:

 

1 mysql编码

 

用下面的命令可以查看mysql当前的各种默认编码:

 

showvariables like 'character/_set/_%'; 可以看到目前mysql的默认编码;showvariables like 'collation_%';
Copy after login

my.cnf中设置对应的编码:

[client]default-character-set=utf8[mysqld]collation_server = utf8_general_cicharacter_set_server = utf8[mysql]default-character-set=utf8[mysqldump]default-character-set=utf8
Copy after login

2 数据库编码:

CREATE DATABASE IF NOT EXISTS my_db DEFAULT CHARSET utf8COLLATE utf8_general_ci;
Copy after login

3. 数据表编码

/*!40101SET @saved_cs_client     =@@character_set_client */;/*!40101SET 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 */;
Copy after login

4. 转存的时候

有时候需要转存数据库,但是好好的数据库转到新库就乱码了,你可能需要:

#mysqldump --default-character-set utf8 -u root -pmypass mydb> mydb.sql
Copy after login

5. 查询的时候

有时候,虽然mysql、数据库、数据表以及导出的数据sql文件编码都没问题,但是query出来仍然出现乱码,可能是因为,在query的时候也需要设置编码,函数名称大同小异,在php+mysql中是:

mysql_query('set names utf8');
Copy after login

 

 

以上。

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!