The same answer for the second table

I have a problem with my code. I have the same answer to my second table. Although in the first he goes on to the next column.

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); ?> 

Here is the answer I get. As you can see the team, teamtwo and s_name are all the same. It does not get the value of the second column.

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

Schedule table enter image description here

Match table enter image description here

+7
json php mysql
source share
2 answers

You can define m_id in the request

 $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
source share

To make sure you get "some kind of answer" even if the data is not properly connected, you need a LEFT CONNECTION:

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

In this context, GROUP BY may not be necessary. Depends on the structure of your data.

0
source share

All Articles