web前端开发 - PHP搜索数据库输出的JSON出现重复

WBOY
Release: 2016-06-06 20:27:48
Original
1358 people have browsed it

PHP使用sql搜索出来的数据,转换成json后就出现重复。
例如下面的0和hps_userid是一样的,1和hps_userid2一样的。
怎么去掉重复的?或直接让sql搜出后,列名为key,值为value?

语句:

<code>$result = mysql_query("select * from hpsubject");
$json = json_encode(mysql_fetch_array($result));
echo $json;</code>
Copy after login
Copy after login

结果:
{"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?

语句:

<code>$result = mysql_query("select * from hpsubject");
$json = json_encode(mysql_fetch_array($result));
echo $json;</code>
Copy after login
Copy after login

结果:
{"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种的效率来说差别不大,但是最后一种实际用起来会灵活的多。

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
Popular Tutorials
More>
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!