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

时间:2016-11-20 10:26:05

标签: php mysql json

所以我从我的php中的两个表中获取数据并将其编码在一个json对象中。我得到了我需要的一切,除了它使显示器加倍。我的teamone位于比赛表中。它不是从数组0开始,而是在调度表之后启动。这是阵列7.我不知道为什么会发生这种情况。

这是我的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);
?>

这是我的展示。如你所见,有四个显示器,但在我的数据库中它只有2.它使显示器加倍。我该如何解决?感谢

{ "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"}]}

我需要从匹配中获取teamone,teamtwo和s_name值,而我需要在一个查询中从调度表中获取开始,结束和场地。 Schedule table

Matches Table

2 个答案:

答案 0 :(得分:0)

我认为这是mysqli_fetch_array()的问题。

Can you please change mysqli_fetch_array()  to mysqli_fetch_assoc()

答案 1 :(得分:0)

由于你的SQL查询,你应该不要忘记来执行某种形式的分组(你从两个表中选择结果的方式定义了这个):

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

或者,您可以将查询重新设计为更具可读性:

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

当然,上面的INNER JOIN可能是LEFT OUTER JOIN - 这完全取决于您的数据库设计。