0

I have two tables: Items:

| Item|
   A
   B
   C

userItems:

| UID |  Item
   1      A
   1      C

This loop shows the data from table Items:

$data = mysql_query("SELECT * FROM `Items`ORDER by `Icons` ASC"); 

while($row = mysql_fetch_array( $data )) 
    {
    echo $row['Item'];
    echo "Unlock";
    }

Basically I need to display Unlocked instead of Unlock if the user with UID = $uid has the item.

This query gets the user items:

$data = mysql_query("SELECT * FROM `userItems` WHERE `UID` = '$uid'"); 

I believe the solution is an left join, but I don't know how to do it. How can I make it work?

4
  • Why do you query from Items table at all? You have Item column in userItems as well. Commented Sep 8, 2011 at 0:42
  • I could insert all items to userItems on registration and add another column but it's easier to have a different table in case I want to add new items in the future. Commented Sep 8, 2011 at 0:43
  • @zerkms I think there's more in the Items table than a single column, Icons for example Commented Sep 8, 2011 at 0:45
  • 1
    @Phil: then it is bad example ;-) programmers solve troubles, not make predictions Commented Sep 8, 2011 at 0:50

1 Answer 1

1
SELECT Items.*, useritems.UID FROM `Items` 
    left join useritems on Items.Item=useritems.Item 
    ORDER by `Icons` ASC

Then just use logic in your PHP to display Unlocked when user $row["items.UID"] equals $uid

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

3 Comments

I'd change the * to Items.*, userItems.UID. Just can't abide by a single * with joined tables. Who knows what you're going to get back
You're right. I have no idea what might be lurking in those tables. ;)
@Liso22 You're kidding! I haven't done SQL in months. I thought for sure I'd have a syntax error or detail lost in there somewhere.

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.