1

I am trying to query the database to:

  1. Get a football match record
  2. Get all the players that are part of that match

There are three tables: Matches | Players | Match_Players

Match_Players simply connects the two other tables together.

I am trying to return the result in JSON.

The problem is with the second query. It returns an Errant Query. This is what I have so far:

$matches = array();
if(mysql_num_rows($matchResult)) 
{
    while($match = mysql_fetch_assoc($matchResult)) 
    {
        $players_query = 
            "SELECT p.* FROM match_players mp 
            LEFT JOIN players p on p.id = mp.player_id
            WHERE mp.id = ".$match->player_id; // <--- This is the error

        $playersResult = mysql_query($players_query,$link) or die('Errant query: '.$players_query);
        $players       = array();

        if(mysql_num_rows($playersResult)) 
        {
            while($player = mysql_fetch_assoc($playersResult)) 
            {
                $players[] = $player;
            }
        }

        $match->$players = $players;    
        $matches[] = $match;
    }
}

Update

I have changed the second SQL statement by replacing $match->player_id to $match['player_id'];. However, I am not receiving the collection of players. All I can see is the match details. Any idea why?

2 Answers 2

2

Use

$match['player_id']

(array syntax) not

$match->player_id

(object syntax) or use

mysql_fetch_object

Though i expect you should do one query, not a loop

Sign up to request clarification or add additional context in comments.

2 Comments

Please check the update. Though the error has gone, I can't see a returned result of the players.
@Subby same problem, change $match->$players = $players; to $match['players'] = $players;
1

Shouldn't the query be like this?

"SELECT p.* 
FROM match_players mp 
RIGHT JOIN players p on p.id = mp.player_id
WHERE mp.match_id = ".$match->id;

or like this

"SELECT p.*
FROM players p, match_players mp, matches m
WHERE p.id = mp.player_id AND mp.match_id = m.id
AND m.id = ". $match->id;

created an sqlfiddle to try and check the queries and they are fine...

7 Comments

I just noticed that too JUST before you posted :) I still don't see the players coming through. If I run this code through PHPMYAdmin, the results come through A-OK.
It throws an errant query when I paste your update in.
Ah, you made a left join which will give you only match_players stuff back... you need to do a right join
Errant query: SELECT p.* FROM match_players mp RIGHT JOIN players p on p.id = mp.player_id WHERE mp.match_id =
this is probably a stupid question but you are putting an id after the = ? :P e.g. "SELECT p.* FROM match_players mp RIGHT JOIN players p on p.id = mp.player_id WHERE mp.match_id = 1"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.