How to solve the problem of garbled data queried by php

jacklove
Release: 2023-03-30 17:52:01
Original
3184 people have browsed it

Garbled data queried by php often occurs when operating the database, so this article will introduce its solution.

First of all, set the encoding method of MySQL 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

Finally Just 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 problem of converting the queried 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 again and it should be fine.

This article introduces the solution to the problem of garbled data queried by PHP. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:

Explain the related operations of orderly splitting of PHP strings

How to use PHP to calculate The distance between two longitudes and latitudes

Explain related examples of PHP array traversal

The above is the detailed content of How to solve the problem of garbled data queried by php. 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 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!