2009-07-28 175 views
1

這一個可能很簡單:PHP/MySQL的Select語句

我有兩個變量:

$sender_id 
$receiver_id 

的存儲和分配給向tblUsers用戶ID的那些。我沒有問題,選擇一個-AT-A-時間:

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

但是,我怎麼會選擇兩行(一個Sender ID和receiverID),這樣我就可以得到在這兩個用戶獲得更多信息。有點像「在SELECT中選擇」。

謝謝!

回答

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

編輯:澄清

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

} 
+0

感謝您的回覆。所以,在我的「while」語句中,如果我想特別指出一個,我將如何區分$ senderID和$ receiverID。 – Dodinas 2009-07-28 16:25:08

+0

我用答案編輯它 – 2009-07-28 16:27:59

2

如果您需要區分哪些是接收器,並且是發送者:

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

這將返回:

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

當然,有兩排,你總是可以的只需比較一下ID即可。如果您的結果集大於兩行,這主要是爲了使縮放更容易。

0

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