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
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'];
}
}
1 Comment
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
SELECT * FROM tblusers WHERE usrID IN ($receiverID, sender_id)