MySQL會(huì)出現(xiàn)中文亂碼的原因不外乎下列幾點(diǎn):
1.server本身設(shè)定問(wèn)題,例如還停留在latin1
2.table的語(yǔ)系設(shè)定問(wèn)題(包含character與collation)
3.客戶端程式(例如php)的連線語(yǔ)系設(shè)定問(wèn)題
強(qiáng)烈建議使用utf8!!!!
utf8可以兼容世界上所有字符!!!!
一、避免創(chuàng)建數(shù)據(jù)庫(kù)及表出現(xiàn)中文亂碼和查看編碼方法
1、創(chuàng)建數(shù)據(jù)庫(kù)的時(shí)候:CREATE DATABASE `test`
CHARACTER SET 'utf8'
COLLATE 'utf8_general_ci';
2、建表的時(shí)候 CREATE TABLE `database_user` (
`ID` varchar(40) NOT NULL default '',
`UserID` varchar(40) NOT NULL default '',
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
這3個(gè)設(shè)置好了,基本就不會(huì)出問(wèn)題了,即建庫(kù)和建表時(shí)都使用相同的編碼格式。
但是如果你已經(jīng)建了庫(kù)和表可以通過(guò)以下方式進(jìn)行查詢。
1.查看默認(rèn)的編碼格式:
mysql> show variables like "%char%";
+--------------------------+---------------+
| Variable_name | Value |
+--------------------------+---------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | utf8 |
| character_set_system | utf8 |
+--------------------------+-------------+
注:以前2個(gè)來(lái)確定,可以使用set names utf8,set names gbk設(shè)置默認(rèn)的編碼格式;
執(zhí)行SET NAMES utf8的效果等同于同時(shí)設(shè)定如下:
SET character_set_client='utf8';
SET character_set_connection='utf8';
SET character_set_results='utf8';
2.查看test數(shù)據(jù)庫(kù)的編碼格式:
mysql> show create database test;
+------------+------------------------------------------------------------------------------------------------+
| Database | Create Database |
+------------+------------------------------------------------------------------------------------------------+
| test | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET gbk */ |
+------------+------------------------------------------------------------------------------------------------+
3.查看yjdb數(shù)據(jù)庫(kù)的編碼格式:
mysql> show create table yjdb;
| yjdb | CREATE TABLE `yjdb` (
`sn` int(5) NOT NULL AUTO_INCREMENT,
`type` varchar(10) NOT NULL,
`brc` varchar(6) NOT NULL,
`teller` int(6) NOT NULL,
`telname` varchar(10) NOT NULL,
`date` int(10) NOT NULL,
`count` int(6) NOT NULL,
`back` int(10) NOT NULL,
PRIMARY KEY (`sn`),
UNIQUE KEY `sn` (`sn`),
UNIQUE KEY `sn_2` (`sn`)
) ENGINE=MyISAM AUTO_INCREMENT=1826 DEFAULT CHARSET=gbk ROW_FORMAT=DYNAMIC |
二、避免導(dǎo)入數(shù)據(jù)有中文亂碼的問(wèn)題
1:將數(shù)據(jù)編碼格式保存為utf-8
設(shè)置默認(rèn)編碼為utf8:
set names utf8;
設(shè)置數(shù)據(jù)庫(kù)db_name默認(rèn)為utf8:
ALTER DATABASE `db_name` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
設(shè)置表tb_name默認(rèn)編碼為utf8:
ALTER TABLE `tb_name` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
導(dǎo)入:
LOAD DATA LOCAL INFILE 'C:\utf8.txt' INTO TABLE yjdb;
2:將數(shù)據(jù)編碼格式保存為ansi(即GBK或GB2312)
設(shè)置默認(rèn)編碼為gbk:
set names gbk;
設(shè)置數(shù)據(jù)庫(kù)db_name默認(rèn)編碼為gbk:
ALTER DATABASE `db_name` DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
設(shè)置表tb_name默認(rèn)編碼為gbk:
ALTER TABLE `tb_name` DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
導(dǎo)入:
LOAD DATA LOCAL INFILE 'C:\gbk.txt' INTO TABLE yjdb;
注:1.UTF8不要導(dǎo)入gbk,gbk不要導(dǎo)入U(xiǎn)TF8;
2.dos下不支持UTF8的顯示;
三、解決網(wǎng)頁(yè)中亂碼的問(wèn)題
將網(wǎng)站編碼設(shè)為 utf-8,這樣可以兼容世界上所有字符。
如果網(wǎng)站已經(jīng)運(yùn)作了好久,已有很多舊數(shù)據(jù),不能再更改簡(jiǎn)體中文的設(shè)定,那么建議將頁(yè)面的編碼設(shè)為 GBK, GBK與GB2312的區(qū)別就在于:GBK能比GB2312顯示更多的字符,要顯示簡(jiǎn)體碼的繁體字,就只能用GBK。
1.編輯/etc/my.cnf ,在[mysql]段加入default_character_set=utf8;
2.在編寫(xiě)Connection URL時(shí),加上?useUnicode=true&characterEncoding=utf-8參;
3.在網(wǎng)頁(yè)代碼中加上一個(gè)"set names utf8"或者"set names gbk"的指令,告訴MySQL連線內(nèi)容都要使用
utf8或者gbk;