Daryl Gill Daryl Gill - 5 months ago 151
MySQL Question

PHP inbox System

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?

Answer
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')