I have a weird problem. When I run this script I get 10 records from the database but they are all exactly the same. I have no idea what I am doing wrong or how to fix it. Please help me.
I have tables AMCMS_highscores , AMCMS_users , AMCMS_games I want to look inside the AMCMS_highscores table, get the last 10 records but only where the field gameid is 1997 for example. Any help is appreciated.
$data = query("SELECT `AMCMS_highscores`.`primkey` , `AMCMS_highscores`.`gameid` , `AMCMS_highscores`.`score` , `AMCMS_users`.`username` , `AMCMS_highscores`.`status` , `AMCMS_highscores`.`userkey` , `AMCMS_games`.`primkey` , `AMCMS_games`.`gamename` FROM `AMCMS_highscores` , `AMCMS_games` , `AMCMS_users` WHERE `AMCMS_highscores`.`gameid` = '$gameid' AND `AMCMS_highscores`.`userkey` != `AMCMS_users`.`userkey` AND `AMCMS_highscores`.`gameid` = `AMCMS_games`.`primkey` AND `AMCMS_highscores`.`status`= 'approved' ORDER by `AMCMS_highscores`.`primkey` DESC LIMIT 0, 10");
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data )) {
Print "<tr>";
Print "<th>Score:</th> <td>".$info['score'] . "</td> ";
Print "<th>ID:</th> <td>".$info['userkey'] . " </td></tr>"; }
Print "</table>";
Here’s a formatted version of the query:
SELECT
`AMCMS_highscores`.`primkey` , `AMCMS_highscores`.`gameid` ,
`AMCMS_highscores`.`score` , `AMCMS_users`.`username` ,
`AMCMS_highscores`.`status` , `AMCMS_highscores`.`userkey` ,
`AMCMS_games`.`primkey` , `AMCMS_games`.`gamename`
FROM `AMCMS_highscores` , `AMCMS_games` , `AMCMS_users`
WHERE `AMCMS_highscores`.`gameid` = '$gameid'
AND `AMCMS_highscores`.`userkey` != `AMCMS_users`.`userkey`
AND `AMCMS_highscores`.`gameid` = `AMCMS_games`.`primkey`
AND `AMCMS_highscores`.`status`= 'approved'
ORDER by `AMCMS_highscores`.`primkey` DESC
LIMIT 0, 10
OOPS, i didnt mean to copy that but i did by accident.
When its with “=” sign (highscores.userkey = users.userkey) i get empty query so that didnt fix it 🙁
I hope this query would be easier to understand 🙂
SELECT highscores.primkey, highscores.gameid, highscores.score, users.username,
highscores.status, highscores.userkey, games.primkey, games.gamename
FROM AMCMS_highscores AS highscores, AMCMS_games as games, AMCMS_users as users
WHERE highscores.gameid = '$gameid' AND
highscores.status = 'approved'
ORDER by highscores.primkey DESC LIMIT 0, 10
Here is the result:
http://www.gamesorbiter.com/FB_app/play.php?gameid=1997
(under the game)
Btw, how do you code your query when you post it ? I clicked the code button and only the first line of the code got coded.
If you are trying to select records from
AMCMS_usersfor which there are no matching row inAMCMS_highscores, then you need to do aLEFT JOIN, and test for NULL values in the joined table. Here’s an extract from JOIN Syntax:Your query would therefore look more like this:
Ignoring the other conditions for a moment, your query is looking at every row in
AMCMS_highscores, and joining it to every row inAMCMS_highscoreswhereAMCMS_highscores.userkeyis not equal toAMCMS_users.userkey. This will produce a lot of matching rows. To see what is happening, remove theLIMITclause from your query.