Home > Article > Backend Development > How to set php mysql encoding
php Mysql encoding setting method: 1. Execute the statement "SET NAMES 'utf8';" before sending the query; 2. Set the statement "character set utf8;" when creating the database; 3. Modify the field encoding It can be "utf8".
The operating environment of this tutorial: windows7 system, php5.6 and mysql5.6.17 version. This method is suitable for all brands of computers.
Recommended: "PHP Video Tutorial"
php mysql setting encoding format
When we access the MySQL database through PHP At this time, even if you set the default character set of the table to utf8 and send the query through UTF-8 encoding, you will find that the data stored in the database is still garbled. The problem lies in the connection layer. The solution is to execute the following sentence before sending the query:
1. SET NAMES 'utf8';
It is equivalent to the following three instructions:
SET character_set_client = utf8; SET character_set_results = utf8; SET character_set_connection = utf8;
2 . Create database
mysql> create database name character set utf8;
3. Create table
CREATE TABLE `type` ( `id` int(10) unsigned NOT NULL auto_increment, `flag_deleted` enum('Y','N') character set utf8 NOT NULL default 'N', `flag_type` int(5) NOT NULL default '0', `type_name` varchar(50) character set utf8 NOT NULL default '', PRIMARY KEY (`id`) ) DEFAULT CHARSET=utf8;
4. Modify the database to utf8.
alter database name character set utf8;
5. Modify the table to use utf8 by default.
alter table type character set utf8;
6. Use utf8.
alter table type modify type_name varchar(50) CHARACTER SET utf8;to modify fields
The above is the detailed content of How to set php mysql encoding. For more information, please follow other related articles on the PHP Chinese website!