Reputation: 5524
I have a problem with creating an inbox System. What i'm trying to do is in "ViewMessages.php" I am trying to pull information from a MYSQL Table to show messages.
My First Statement is:
$MessageQuery = mysql_query("SELECT * FROM messages WHERE ToUName='$ToUName' AND FromUName='$FromUName'");
But I realised a flaw, it will only show messages sent 1 way. I Tried something like:
$MessageQuery = mysql_query("SELECT * FROM messages WHERE ToUName='$ToUName' AND FromUName='$FromUName' OR FromUName='$ToUName' AND ToUName='$FromUName'");
This failed. Can anyone shed some light to show both messages from both parties?
Upvotes: 0
Views: 986
Reputation: 204924
SELECT *
FROM messages
WHERE '$ToUName' in (ToUName, FromUName)
OR '$FromUName' in (ToUName, FromUName)
or if you prefer columns listed first in your query
SELECT *
FROM messages
WHERE ToUName in ('$ToUName', '$FromUName')
OR FromUName in ('$ToUName', '$FromUName')
Upvotes: 1
Reputation: 2615
Try This:
$MessageQuery = mysql_query("SELECT * FROM messages WHERE ToUName='$ToUName' || FromUName='$FromUName'");
Upvotes: 0
Reputation: 543
How about union?
$MessageQuery = mysql_query("(SELECT * FROM messages WHERE ToUName='$ToUName' AND FromUName='$FromUName') UNION (SELECT * FROM messages WHERE FromUName='$ToUName' AND ToUName='$FromUName')");
Note: If you need messages in any particular order, you can use ORDER BY
at the end of the query, hoping you have something like message_id or timestamp attached to each.
Upvotes: 3
Reputation: 764
You have boolean operators mixed up, try put some () into that.. (a AND b) OR (c AND d).
Also what you'd acomplish is getting all messages between you and the one other contact. Are you aware of that?
Upvotes: 1