0

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?

2
  • 1
    SQL table structure, or it didn't happen. Commented Nov 5, 2012 at 14:49
  • 2
    You are using an obsolete database API and should use a modern replacement. Don't write new code with mysql_*. Commented Nov 5, 2012 at 14:50

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

You have mentioned the PHP $variable in the column place.
I have just tried this method, i'm using $_GET to get the information for the to and from, and if i modified ToUName to "testing" and kept the FromUName, it returned the results from the old variables.
SELECT * FROM messages WHERE ToUName in ('$ToUName', '$FromUName') AND FromUName in ('$ToUName', '$FromUName') just modified to that and it works. Thankyou
1

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?

1 Comment

That's what I want. Its a "view message" script, where you can view the conversation between both parties
0

Try This:

$MessageQuery = mysql_query("SELECT * FROM messages WHERE ToUName='$ToUName' || FromUName='$FromUName'");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.