dongyi1748 2016-11-20 10:26
浏览 32
已采纳

为什么我在json中加倍我的显示

So i fetch my data from two tables in my php and encode it in one json object. I got everything i needed except that it doubles the display. And my teamone is located in the matches tables. instead of starting from array 0, it starts after the schedules tables. Which is array 7. I dont know why this happen.

Here is my php.

  $sql = "SELECT * from schedule, matches;";  
 $con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name); 

 $result = mysqli_query($con,$sql);


 $response = array();
while($row=mysqli_fetch_array($result))
{
array_push($response, array("start"=>$row[4],"end"=>$row[5],"venue"=>$row[6], "teamone"=>$row[8], "teamtwo"=>$row[9], 

"s_name"=>$row[17]));

}


echo json_encode (array("schedule_response"=>$response));

mysqli_close($con);
?>

Here is my display. As you can see there are four displays but in my database it only has 2. It doubles the display. How do i fix this? Thanks

{ "schedule_response":[
             {"start":"2016-11-10 00:00:00","end":"2016-11-04 00:00:00","venue":"bbbb","teamone":"aaa","teamtwo":"bbb",
            "s_name":"hehehe"},
            {"start":"2016-11-23 00:00:00","end":"2016-11-24 00:00:00","venue":"bbbbbbbb","teamone":"aaa","teamtwo":"bbb",
            "s_name":"hehehe"},
            {"start":"2016-11-10 00:00:00","end":"2016-11-04 00:00:00","venue":"bbbb","teamone":"ehe","teamtwo":"aha",
            "s_name":"aaa"},
            {"start":"2016-11-23 00:00:00","end":"2016-11-24 00:00:00","venue":"bbbbbbbb","teamone":"ehe","teamtwo":"aha",
            "s_name":"aaa"}]}

I need to get the teamone, teamtwo and s_name values from the matches while i need the start, end and the venue from the schedule table in one query. Schedule table

Matches Table

  • 写回答

2条回答 默认 最新

  • drh96824 2016-11-20 11:46
    关注

    Because of your SQL query, you should not forget to perform some form of a grouping (the way you select results from both table defines that):

     $sql = "SELECT * from schedule s, matches m GROUP BY s.id";  //I assume that your table schedule has an `id`
    

    Or, you can rework the query to be more readable:

    $sql = "SELECT s.*, m.* FROM schedule s 
           INNER JOIN matches m ON m.schedule_id = s.id 
           GROUP BY s.id"; //I assume that you have such database design that you have defined foreign key on table `matches`.
    

    Of course, the INNER JOIN above could be LEFT OUTER JOIN - it all depends on your database design.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划