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
1. The occurrence of the problem
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.
2. Troubleshooting process
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.
3. Solution
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.
4. Summary
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)
