PHP/MySQL Select Statement

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!

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'];
    }

}

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.

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

Related Articles
  • I have a SQL routine written that compares a user's input with an existing value in a table. If the value exists, a green check mark appears next to the input. If it doesn't, then a red x appears. My SQL statement is as follows: $check = $con->prepar
  • This question already has an answer here: PHP/MySQL using an array in WHERE clause 17 answers I have created an array in php containing a few USER_IDs. I am using mysql database having a table named USERS. Now I want to select only those rows from US
  • I have an issue where i have a textarea in a form where users can enter names on seperate lines. On submit I explode the "\n". I then want to pass the values from the array into an Select statement, but when i run the script it only returns one
  • I was planning on making an HTML form. The form will display the current table in the database as shown below Toy | Color Ball | Red Bat | Yellow Glove| Brown At the end of the form, it should say "Make a query that gets all the toys and its respecti
  • I'll try to detail this the best I can. I have a nested select statement with a where in clause, but the nested part of the select should be interpreted as a literal string (I believe this is the right terminology). However the default behavior of my
  • I have used the PHP Gantt Class from Github here: https://github.com/bastianallgeier/gantti I have a php script which generates a gantt chant from data in a php array: $data = array(); $data[] = array( 'label' => 'Project 1', 'start' => '2012-04-20'
  • Yesterday I have a question, just this unanswered. I have a php login and register script. When the users register they get in the database is automatically assigned an ID (auto-increment). This ID will in another table linked to a virtual machine (s
  • I'm trying to call my function, but she's wrong. I believe it is in connection variable. Connection: $conn = mysqli_connect('','','', ''); if(mysqli_connect_errno()) { header("Location: error.php"); exit(); } Function: function t_car($id) { glob
  • This question already has an answer here: Reference - What does this error mean in PHP? 28 answers I am receiving this message "Fatal error: Call to a member function bind_param() on a non-object in C:\xampp\htdocs\pcms\includes\blog.php on line 65&q
You Might Also Like