Home  >  Article  >  Backend Development  >  What should I do if php saves data to the database with garbled characters?

What should I do if php saves data to the database with garbled characters?

藏色散人
藏色散人Original
2021-09-18 10:08:112175browse

Solution to garbled data when php saves data to the database: 1. Add "header("Content-Type: text/html; charset=utf-8");" to the php file; 2. Set it through mysql_query mysql encoding; 3. Set HTML file encoding.

What should I do if php saves data to the database with garbled characters?

The operating environment of this article: Windows7 system, PHP7.1 version, Dell G3 computer

What should I do if php saves data to the database with garbled characters? ?

php Chinese garbled characters saved in mysql database

Recently there is a php project, garbled characters are a headache problem

Solution:

1, add header("Content-Type: text/html; charset=utf-8");

2, add mysql_query(" before the PHP program that needs to perform database operations) in the php file set names 'encoding'");, the encoding is consistent with the php encoding. If the php encoding is gb2312, then the mysql encoding is gb2312. If it is utf-8, then

mysql encoding is utf8, so when inserting or retrieving data There will be no garbled characters

mysql_query("set names 'utf8'") 或者mysqli_query("set names 'utf8'");

It should be noted that the place marked in red is utf8, not utf-8;

3, the encoding of the html file used must also be consistent, generally use utf-8 ;

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What should I do if php saves data to the database with garbled characters?. 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