[web] Query grabs the wrong row.. not sure why?

Started by
1 comment, last by CelticSir 16 years, 1 month ago
I have used an inner join on 2 tables on ItemID but for some unknown reason its getting the wrong row but I don't see how. From the logic i have placed in, it cannot be possible to get the result it is getting, but im sure I've done some silly mistake in the query that i have not spotted so maybe some one here can:

<?php
$Get = mysql_query("SELECT useritem.ItemID,useritem.Quantity FROM useritem 
            INNER JOIN item ON item.ItemID WHERE item.Trade=1 AND
                    useritem.UserID='{$_SESSION['Current_User']}' 
                    AND useritem.Quantity>0")
                        Or die(mysql_error());
?>
I'll show ya the table layout i have with 2 items in ... one is the incorrect one which is ItemID 2. Item 1 is the row that this query is meant to get. But sadly it grabs the ItemID 2. The reason ItemID2 is incorrect is because Trade is = 0 and the query asks for items with trade=1 which is the only difference in my database. So i don't know why it's collecting that itemID2 row instead of the ItemID1... User item table:
Quote: ItemId | Quantity | UserID | ---1--- |-----5-----|----1-----| ---2---|-----1-----|----1-----|
Item table
Quote: ItemID | Trade | ---2---|-----0-----| ---1---|-----1-----|
Can any one see what I did wrong?
Advertisement
Here's my guess. Change highlighted in bold

<?php$Get = mysql_query("SELECT useritem.ItemID,useritem.Quantity FROM useritem             INNER JOIN item ON item.ItemID = useritem.ItemID WHERE item.Trade=1 AND                    useritem.UserID='{$_SESSION['Current_User']}'                     AND useritem.Quantity>0")                        Or die(mysql_error());?>

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

Quote:Original post by Sander
Here's my guess. Change highlighted in bold

<?php$Get = mysql_query("SELECT useritem.ItemID,useritem.Quantity FROM useritem             INNER JOIN item ON item.ItemID = useritem.ItemID WHERE item.Trade=1 AND                    useritem.UserID='{$_SESSION['Current_User']}'                     AND useritem.Quantity>0")                        Or die(mysql_error());?>


Argh i see! It's working now ! Thank you very much Sander!

This topic is closed to new replies.

Advertisement