How to solve the problem that the data queried by PHP is garbled and the Chinese code becomes Unicode when converted to json?

jacklove
Release: 2023-03-27 15:08:02
Original
2541 people have browsed it

This article explains the garbled data queried by PHP and the encoding problem when Chinese becomes Unicode when converted to json.

First of all, the encoding method of MySQL must be set to utf8,

Secondly, the encoding of writing to the database and the encoding of reading the database must also be unified to utf8

mysqli_query($conn,"set character set 'utf8'");
mysqli_query($conn,"set names 'utf8'");
Copy after login

The last thing is to set the head and write it at the top of the document.

header('content-type:text/json;charset=utf-8');
Copy after login

If you encounter the query, When converting the data to json, use JSON_encode($data);

You will find that Chinese is actually installed as Unicode. Fortunately, a configuration appeared after 5.4. JSON_UNESCAPED_UNICODE

Case

$json = JSON_encode($resoult_all,JSON_UNESCAPED_UNICODE);
Copy after login

Try outputting it again and you will be surprised! ! !

This article explains the garbled data queried by php and the encoding problem when Chinese becomes Unicode when converted to json.

For more related content, please pay attention to the php Chinese website.

Related recommendations:

PHP array classification, array creation example explanation

php pdo placeholder usage (code Example explanation)

PHP multi-dimensional array sorting array detailed explanation

The above is the detailed content of How to solve the problem that the data queried by PHP is garbled and the Chinese code becomes Unicode when converted to json?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!