web前端开发 - PHP输出json到web无法正常显示中文
PHP从mysql获取数据后,录入数组并转为json。
最后输出的时候,中文显示为"?"。
这个问题是出在服务器还是php?
在数据库里看,中文是能正常显示的。
PHP代码:
<code> $result = mysql_query("select * from hpsubject"); while ($row = mysql_fetch_array($result)) { $arr = json_encode(array('hps_userid'=>$row['hps_userid'], $row['hps_userid2'], $row['hps_subid'], $row['hps_type'], $row['hps_addr'], $row['hps_distance'], $row['hps_title'], $row['hps_feel'], $row['hps_dowhat'], $row['hps_text'], $row['hps_img'], $row['hps_like'], $row['hps_repost'], $row['hps_comment'], $row['hps_date'], $row['hps_time'], $row['hps_liveline'], $row['hps_deadline'])); echo $arr; }</code>
输出结果:
{"hps_userid":"1","0":"1","1":"1","2":"????","3":"??","4":"50","5":"??","6":"??","7":"????","8":"??","9":"??","10":"100","11":"100","12":"100","13":"2015-09-21","14":"154436","15":"2015-09-21 15:44:36","16":"2015-09-21 15:44:36"}
回复内容:
PHP从mysql获取数据后,录入数组并转为json。
最后输出的时候,中文显示为"?"。
这个问题是出在服务器还是php?
在数据库里看,中文是能正常显示的。
PHP代码:
<code> $result = mysql_query("select * from hpsubject"); while ($row = mysql_fetch_array($result)) { $arr = json_encode(array('hps_userid'=>$row['hps_userid'], $row['hps_userid2'], $row['hps_subid'], $row['hps_type'], $row['hps_addr'], $row['hps_distance'], $row['hps_title'], $row['hps_feel'], $row['hps_dowhat'], $row['hps_text'], $row['hps_img'], $row['hps_like'], $row['hps_repost'], $row['hps_comment'], $row['hps_date'], $row['hps_time'], $row['hps_liveline'], $row['hps_deadline'])); echo $arr; }</code>
输出结果:
{"hps_userid":"1","0":"1","1":"1","2":"????","3":"??","4":"50","5":"??","6":"??","7":"????","8":"??","9":"??","10":"100","11":"100","12":"100","13":"2015-09-21","14":"154436","15":"2015-09-21 15:44:36","16":"2015-09-21 15:44:36"}
if (function_exists('mysql_set_charset') === false) { mysql_query("SET NAMES 'utf8'", $con); }else{ mysql_set_charset('utf8', $con); }
$con
对应 mysql_connect
函数的返回值, 上面的 utf8
改为你数据库的编码(上面的代码放在你执行mysql_query("select * from hpsubject")
之前.
然后在你的php
文件头部增加一行header('Content-Type: application/json; charset=utf-8');
其中这里的 utf-8
也改为你数据库中所使用的编码.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
