第二个表的响应相同

时间:2016-11-21 07:52:04

标签: php mysql json

我的代码有问题。我对第二张表的反应相同。在第一个中它进入下一列。

PHP

$sql =  "SELECT * from schedule s, matches m GROUP BY s.id";
 $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);
?>

这是我得到的回应。正如你可以看到teamone,teamtwo和s_name都是一样的。它没有得到第二列的值。

{"schedule_response":[
{"start":"2016-11-23 00:00:00","end":"2016-11-24 00:00:00","venue":"bbbb",
 "teamone":"aaa","teamtwo":"hehe","s_name":"sssss"},
{"start":"2016-11-22 00:00:00","end":"2016-11-23 00:00:00","venue":"aaaaaaa",
"teamone":"aaa","teamtwo":"hehe","s_name":"sssss"}]}

安排表 enter image description here

匹配表 enter image description here

2 个答案:

答案 0 :(得分:1)

您可以在查询中定义m_id

$sql =  "SELECT * from schedule as s, matches as m where s.m_id = m.m_id GROUP BY s.id";

enter image description here

答案 1 :(得分:0)

为了确保即使没有正确链接数据也能获得“一些答案”,您需要我们左键加入:

SELECT * 
FROM schedule s
LEFT JOIN matches m ON s.m_id=m.m_id

在这种情况下,GROUP BY可能是不必要的。取决于您的数据结构