1

This one's probably easy:

I have two variables:

$sender_id
$receiver_id

Those ID's are stored and assigned to a user in tblusers. I have no problem selecting one-at-a-time:

$data = mysql_query("SELECT * FROM tblusers WHERE usrID='$receiverID'") or die(mysql_error());

while($row = mysql_fetch_array( $data ))
{
echo $row['usrFirstName'];
echo $row['usrLastName'];
}

However, how would I select both rows (one for senderID and receiverID) so that I can gain access to further information on both those users. Sort of like a "SELECT within a SELECT".

Thanks!

3 Answers 3

5
SELECT * FROM tblusers WHERE usrID='$receiverID' or usrID='$sender_id'

EDIT: clarification

while($row = mysql_fetch_array( $data ))
{
    if($row['usrID'] == $receiverID) {
        echo "Receiver: " . $row['usrFirstName'] . " " . $row['usrLastName'];
    } else {
        echo "Sender: " . $row['usrFirstName'] . " " . $row['usrLastName'];
    }

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

1 Comment

Thanks for the reply. So, in my "while" statement, how would I differentiate between the $senderID and $receiverID, if I wanted to refer to one specifically.
2

If you need to differentiate which is the receiver and which is the sender:

select
    'Receiver' as UserType,
    *
from
    users
where
    usrid = $receiver_id
union all
select
    'Sender' as UserType,
    *
from
    users
where
    usrid = $sender_id

This will return:

UserType  |  UsrID  |  Name
Receiver  |   23    |  John Smith
Sender    |   42    |  Adam Douglas

Of course, with two rows, you can always just compare the ID's to figure that out, too. This is mainly to make scaling easier if you have a larger result set than just two rows.

Comments

0

SELECT * FROM tblusers WHERE usrID IN ($receiverID, sender_id)

Comments

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.