PHP MYSQL garble problem using SET NAMES utf8 correction
- 2020-03-31 17:07:28
- OfStack
Write it down first, in case you can't remember to look for it again!
When PHP operates on a database, the data in the database is encoded in UTF8. Question mark garble code, found some data before the original is to read a coding setting:
Before inserting data:
then
Mysql_query (" insert into tablename..." )
Execute before reading the data:
The same code at the page code block index 1
Then mysql_query("select * from tablename")
Note: the code read here is the original encoding of the content of the output after re-coding, such as the output content of the page is GBK code, so when the page is read in the display is also a messy code, so before the query execute mysql_query("SET NAMES GBK "), in the page can normally display GBK code of the text content.
When PHP operates on a database, the data in the database is encoded in UTF8. Question mark garble code, found some data before the original is to read a coding setting:
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;
Before inserting data:
mysql_query("SET NAMES utf8");
then
Mysql_query (" insert into tablename..." )
Execute before reading the data:
The same code at the page code block index 1
Then mysql_query("select * from tablename")
Note: the code read here is the original encoding of the content of the output after re-coding, such as the output content of the page is GBK code, so when the page is read in the display is also a messy code, so before the query execute mysql_query("SET NAMES GBK "), in the page can normally display GBK code of the text content.