php从mysql获取数据后,录入数组并转为json。
最后输出的时候,中文显示为"?"。
这个问题是出在服务器还是php?
在数据库里看,中文是能正常显示的。
php代码:
$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;
}输出结果:
{"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代码:
$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;
}输出结果:
{"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免费学习笔记(深入)”;
然后在你的php文件头部增加一行header('Content-Type: application/json; charset=utf-8'); 其中这里的 utf-8 也改为你数据库中所使用的编码.
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
C++高性能并发应用_C++如何开发性能关键应用
Java AI集成Deep Java Library_Java怎么集成AI模型部署
Golang后端API开发_Golang如何高效开发后端和API
Python异步并发改进_Python异步编程有哪些新改进
C++系统编程内存管理_C++系统编程怎么与Rust竞争内存安全
Java GraalVM原生镜像构建_Java怎么用GraalVM构建高效原生镜像
Python FastAPI异步API开发_Python怎么用FastAPI构建异步API
C++现代C++20/23/26特性_现代C++有哪些新标准特性如modules和coroutines
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号