• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

web前端开发 – PHP输出json到web无法正常显示中文

php 搞代码 4年前 (2022-01-25) 10次浏览 已收录 0个评论
文章目录[隐藏]

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″}

<code class="php">if (function_exists('mysql_set_charset') === false) {   mysql_query("SET NAMES 'utf8'", $con);}else{   mysql_set_charset('utf8', $con);}</code>

$con 对应 mysql_connect 函数的返回值, 上面的 utf8 改为你数据库的编码(上面的代码放在你执行mysql_query("select * from hpsubjec

。本文来源gao!%daima.com搞$代*!码网1

搞代gaodaima码t")之前.

然后在你的php文件头部增加一行header('Content-Type: application/json; charset=utf-8'); 其中这里的 utf-8 也改为你数据库中所使用的编码.


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:web前端开发 – PHP输出json到web无法正常显示中文
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址