Recently, you have encountered database coding problems one after another. It makes your system feel like a beautiful girl but suddenly finds that she is illiterate. In fact, many times it is a coding problem, and mysql (especially) has many places for coding, so do it here A screening:
1 mysql encoding
Use the following command to view the various default encodings of mysql :
showvariables like 'character\_set\_%'; you can see the current default encoding of mysql ;
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 encoding :
CREATE DATABASE IF NOT EXISTS my_db DEFAULT CHARSET utf8COLLATE utf8_general_ci;
3. Data table encoding
/*!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 */;
4. When dumping
Sometimes it is necessary to dump the database, but the good database will be garbled when it is transferred to the new database. You may need to:
#mysqldump --default-character-set utf8 -u root -pmypass mydb> mydb.sql
5. When inquiring
Sometimes, although the encoding of mysql, database, data table, and exported data sql file is all right, there are still garbled characters in the query, which may be because the encoding needs to be set when querying, and the function names are similar. In php+mysql yes:
mysql_query('set names utf8');
that's all.