时间:2021-07-01 10:21:17 帮助过:13人阅读
语句:
$result = mysql_query("select * from hpsubject");
$json = json_encode(mysql_fetch_array($result));
echo $json;
结果:
{"0":"1","hps_userid":"1","1":"1","hps_userid2":"1","2":"154436","hps_time":"154436"}
PHP使用sql搜索出来的数据,转换成json后就出现重复。
例如下面的0和hps_userid是一样的,1和hps_userid2一样的。
怎么去掉重复的?或直接让sql搜出后,列名为key,值为value?
语句:
$result = mysql_query("select * from hpsubject");
$json = json_encode(mysql_fetch_array($result));
echo $json;
结果:
{"0":"1","hps_userid":"1","1":"1","hps_userid2":"1","2":"154436","hps_time":"154436"}
mysql_fetch_row //只保留数字0,1,2,3,4,5,6……的键
mysql_fetch_assoc //只保留字符串(表的列名)的键
mysql_fetch_array //两种都要
通常用最后一种,因为3种的效率来说差别不大,但是最后一种实际用起来会灵活的多。