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

PHP reads database garbled code

王林
Release: 2023-04-07 14:34:01
Original
4080 people have browsed it

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

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

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!

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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template