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

时间:2023-01-24 09:09:28

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

我有一个查询从PHP发送到我的SQL数据库。它看起来是这样的:

$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.

我最终将json_encoded结果返回到我的Java应用(实际上是Android)中,这样我就可以把它们分开,放到一个漂亮的Java对象中。

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.

我已经到了需要从这个查询中获得更多数据的地步,我不知道如何获得它。是否有一种方法可以让我自己给自己另一个查询的结果,并将其注入到json_encoded结果中?就像运行另一个查询,并在循环的每次迭代中将它附加到$r中,并让它看起来像原始查询select返回的另一个列?我只是不知道如何处理这个$rows[]数组。

Let me know if my question is not clear.

如果我的问题不清楚,请告诉我。

1 个解决方案

#1


0  

Add mutiple levels to your PHP array:

向您的PHP数组添加多个级别:

$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.

然后只需在客户端代码中引用这些子数组。

#1


0  

Add mutiple levels to your PHP array:

向您的PHP数组添加多个级别:

$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.

然后只需在客户端代码中引用这些子数组。