Home  >  Article  >  Backend Development  >  PHP reads database garbled code

PHP reads database garbled code

王林
王林Original
2019-10-10 17:50:594013browse

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:

PHP reads database garbled code

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!

Statement:
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