Home  >  Article  >  Backend Development  >  Garbled characters occur after PHP connects to mysql database

Garbled characters occur after PHP connects to mysql database

不言
不言Original
2018-04-19 15:27:504602browse

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!

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