Home  >  Article  >  Backend Development  >  How to set php mysql encoding

How to set php mysql encoding

藏色散人
藏色散人Original
2020-11-19 09:21:022181browse

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".

How to set php mysql encoding

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!

Statement:
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
Previous article:How to call php from jsNext article:How to call php from js