2

I have 2 tables, a users table and a trade table.

Which look like:

enter image description here

The structure of my code right now is:

<?php 
$history = mysqli_query($con, "SELECT * FROM .......");

while($row = mysqli_fetch_array($history)) { 
echo("The sentence");
} ?>

Problem I'm facing is that I'm trying to echo the user_name which in one case has to be the receiver and other the person giving it.

5
  • 1
    Have you specified what you select from both table rather than use "*" selector and surround $fbid with single quote '$fbid' ? Commented Oct 2, 2014 at 0:33
  • I haven't because I think it wouldn't make sense since the 2 values I'm looking for are both users.user_name. And the '' around $fbid shouldn't make an impact, at least I've tested both. Commented Oct 2, 2014 at 1:21
  • Sorry, but I do not support questions with the deprecated mysql_* extensions. Commented Oct 2, 2014 at 1:21
  • What's the structure of your item_send table? You only mentioned the user and trade table. And not sure but, is it alright not to use apostrophe (') on your $fbid variable inside the query? Correct me if I'm wrong. Commented Oct 2, 2014 at 1:33
  • I've deleted some crappy code @ZombieHunter and I've added a more clear picture of what the structure and goal is LoganWayne. Thanks for your help. Commented Oct 2, 2014 at 1:50

1 Answer 1

4

Pro tip: Never use SELECT * in software unless you know exactly why you are doing so. In your case it is harmful.

I'm assuming your query is really against the user and trade tables you mentioned in your question.

First, recast your query using 21st century SQL, as follows:

SELECT * 
  FROM trade AS t
  JOIN user AS s ON  s.user_id = t.user_id_sender
 WHERE s.facebook_id = $fbid 

Second, use this to retrieve your user's names and the item id traded.

SELECT s.user_name AS sender,
       r.user_name AS receiver,
       t.trade_id AS item_id
  FROM trade AS t
  JOIN user AS s ON  s.user_id = t.user_id_sender
  JOIN user AS r ON  r.user_id = t.user_id_receiver
 WHERE s.facebook_id = $fbid 

See how we JOIN the user table twice, with two different aliases s (for sender) and r (for receiver)? That's the trick to fetching both names from IDs.

See how we employ the aliases sender and receiver to disambiguate the two user_name columns in the result set?

Now, when you use the php fetch_array function, you'll end up with these elements in the array.

$history['sender']
$history['receiver']
$history['item_id']

The array index strings correspond to the alias names you specified in your SELECT clause in your query.

So, one reason to avoid SELECT * is that you can get more than one column with the same name, and that means fetch_array will eliminate those duplicates and so it will lose useful information from your result set.

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

1 Comment

This is one of the better illustrations of aliases I've seen. Every once in a while you bump into something that, $understanding++; Thanks!

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.