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

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

jacklove
jackloveOriginal
2018-05-22 17:56:192678browse

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'");

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');

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);

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!

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