PHP mysqli Chinese garbled solution: 1. Check and modify the encoding of the html file; 2. Modify the sorting rule of the mysql database to "utf8_general_ci"; 3. Add the setting encoding before executing the SQL statement.
#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.
What should I do if Chinese garbled characters appear in PHP mysqli?
PHP mysqli newly added data has Chinese garbled errors
When using PHP mysqli to connect to the database and write data, I found that the Chinese written to the database was garbled. I guess this is an encoding problem.
1. File structure
File name | Remarks |
---|---|
addbook.html | Form submission data web page |
addbook.php | Execute sql statement php file |
common.php | storage mysql database host and other configuration data |
2. First check html file, make sure to use <meta charset="UTF-8">
3. Secondly, check the mysql database, the character set is utf8
, sort The rule is utf8_general_ci
4. Finally, I checked the PHP file and finally found the problem
I just printed the Chinese data passed by the form and found that there was no garbled code.
So the remaining problem is that the code to connect to the database is not properly coded.
Key points:Add setting encoding before executing the SQL statement
// 设置编码 mysqli_query($conn,"set names utf8"); // 执行sql语句 $rs = mysqli_query($conn,$sql); // 关闭流 mysqli_close($conn);
Never put mysqli_query($conn ,"set names utf8");
If placed after executing the sql statement, it will still be invalid.
If there is an error in the program, look for it slowly and be patient. If you can't find the error, you can put it aside and do other things, and maybe you'll figure it out.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What to do if PHP mysqli Chinese is garbled?. For more information, please follow other related articles on the PHP Chinese website!