What to do if PHP mysqli Chinese is garbled?

藏色散人
Release: 2023-03-14 10:52:01
Original
3587 people have browsed it

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.

What to do if PHP mysqli Chinese is garbled?

#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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template