Home > Article > Backend Development > What should I do if php saves data to the database with garbled characters?
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.
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!