【发布时间】:2015-01-23 08:42:14
【问题描述】:
我尝试通过 php 从我的 mysql 数据库中获取数据。我想使用 json,因为它在我们的 android 应用程序中处理起来非常好。
我尝试以下方法: 我们有三个数据表:
User:
-id
-name
REF_Interests
-ref_interests_id
-interestName
User_Ref_Interests
-id
-userid
-ref_interests_id
我们创建了多个用户和兴趣,例如 ref_interests_id = 1(例如)的“游泳”
如何才能为每个用户获取一个 Row,其中的数组包含这样的兴趣:
User:
-id
-name
-interests:
--interest
---ref_interest_id
---interestName
--interest
---ref_interest_id
---interestName
--...
---...
---...
如果该用户有 4 个兴趣,我加入为该用户返回 4 行的表。我认为这很难处理。那么有没有比我的实际解决方案更好的方法?
谢谢你千百次!
【问题讨论】:
-
您是否尝试使用 php 函数 json_encode() 将数组编码为 jason?
-
是的,我正在对其进行编码。但是我的 SQL 查询为用户的每个兴趣返回多行
-
对于用户,一旦您获得多行的结果集,就在结果集上运行 foreach 循环并捕获数组中的值,然后将其编码为 json。