Home > Backend Development > PHP Tutorial > PHP operates MYSQL garbled code and uses SET NAMES utf8 to correct it_PHP tutorial

PHP operates MYSQL garbled code and uses SET NAMES utf8 to correct it_PHP tutorial

WBOY
Release: 2016-07-21 14:55:24
Original
827 people have browsed it

Write it down first so that you don’t have to remember it and look for it everywhere later!

When PHP operates the database, the data in the database is encoded in UTF8. When it is read out, all that is displayed is garbled question marks . I found some information and it turned out that I was reading it. Make encoding settings before:

create table tablename

(
id int not null auto_increment,
title varchar(20) not null,
contnet varchar(300) defalut null,
primary key ('id')
)begin =MyISAM DEFAULT CHARSET =UTF8;

Execute before inserting data:

mysql_query("SET NAMES utf8");

Then mysql_query("insert into tablename....")

Execute before reading data:

mysql_query("SET NAMES utf8");

Then mysql_query("select * from tablename")

Note: The encoding read here is the output after re-encoding the original encoded content. For example, the page where the output content is located is GBK encoded, then the page display will also be garbled when reading, so before querying Execute mysql_query("SET NAMES gbk"), and the GBK encoded text content

can be displayed normally on the page.

Reprinted from: http://www.cnblogs.com/zazl/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364415.htmlTechArticleWrite it down first so that you don’t have to remember it and look for it everywhere later! When PHP operates the database, the data in the database is used UTF8 encoding, when I read it out, all it showed was ??????? question mark garbled characters, I found some...
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