Home > Article > Backend Development > PHP reads database garbled code
When reading data from the background, garbled characters usually appear, such as "Chinese characters" become "?", etc. The reason for this is usually incorrect encoding settings. The solution is As follows:
First method:
Add the following code in php, set the encoding format to "utf-8", the code is as follows:
header("Content-Type: text/html; charset=UTF-8");
Second method:
Add another line of code in php, which is also used for transcoding. The code is as follows:
$conn = mysqli_connect($servername, $username, $password, $mysqlname); $conn->query("SET NAMES utf8");
In this case, create it first Link and then transcode.
In addition, when using a database, if you create a table directly manually (not with code), usually when you enter Chinese characters in the table, they cannot be displayed or are displayed as "?" when browsing. The reason for this is also coding. The problem and solution are as follows:
When building a table or database, the encoding format of the table and database must be unified and set to: "utf8_general_ci", as shown below:
Recommended tutorial: PHP video tutorial
The above is the detailed content of PHP reads database garbled code. For more information, please follow other related articles on the PHP Chinese website!