Home >Backend Development >PHP Tutorial >Garbled characters occur after PHP connects to mysql database
This article mainly introduces the problem of garbled characters after PHP connects to the mysql database. It has a certain reference value. Now I share it with you. Friends in need can refer to it.
Generally speaking, it appears There are two cases of Chinese garbled characters:
One: The PHP code and the database encoding method are inconsistent:
General ideas to solve this problem: Reset the encoding format of the database:
Solution The specific solution to the problem: 1. Change the encoding format of the database configuration file to be consistent with the encoding format used by your PHP code.
2. After getting the $conn value, set the encoding format for the returned object (resource) $conn (taking character encoding utf-8 as an example): mysqli_set_charset($conn, "utf-8" ).
2: The encoding format used by the PHP code is inconsistent with the encoding format used by the browser for decoding:
Solution to the problem: Will be in the returned http response Set the browser's decoding method in the message.
Specific ways to solve the problem: 1. Add code at the beginning of the file: header("Content-Type: text/html;charset:utf-8")
The above is the detailed content of Garbled characters occur after PHP connects to mysql database. For more information, please follow other related articles on the PHP Chinese website!