douliang4858 2013-04-28 02:54
浏览 23

如何将更多数据附加到从SQL查询创建的JSON编码数组?

I have a query that is sent to my SQL database from PHP. It looks something like this:

$result = mysql_query("SELECT ...");
while($r = mysql_fetch_assoc($result)) {
    $rows[] = $r;
}
print json_encode($rows);

I ultimately return the json_encoded result to my Java application (Android, actually) so that I can pick apart the pieces and put them into a nice Java object.

I have come to a point where I need some more data to come from this query, and I don't know how to get it. Is there a way for me to give myself the results of ANOTHER query, and inject it into this json_encoded result? Like basically run another query, and append it to the $r on each iteration of the loop and have it "seem" as if it was another column returned from the original queries select? I just don't know how to handle this $rows[] array.

Let me know if my question is not clear.

  • 写回答

1条回答 默认 最新

  • douyi2664 2013-04-28 02:56
    关注

    Add mutiple levels to your PHP array:

    $data = array();
    
    $data['part1'] = 'data from query 1';
    $data['part2'] = 'data from query 2';
    
    echo json_encode($data);
    

    then simply refer to those sub-arrays in your client-side code.

    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私