0

I have this PM system, where users can write each other. When I show the available PM's I have in my MySQL table deleted_sender and deleted_receiver

If the deleted_sender is 1, then the person who sent the PM shouldn't be able to see it in the list. If the deleted_receiver is 1, then the person who received the PM shouldn't be able to see it.

My question is, how can I do this with a mysql query? Currently I have this:

$r=mysql_query("
         SELECT * FROM private_messages 
         WHERE 
           sender_id='".$userdata['id']."' 
         OR 
           receiver_id='".$userdata['id']."' 
         AND 
           (deleted_receiver='0' OR deleted_sender='0')");

Although, this just shows the PM at both the sender and the receiver.

2
  • 2
    I am not sure why you want to write single query, see both sender or reciever are seperate entity, and you only want to show a result to either of them. if it is deleted for sender, then don't show to sender by show to reciever. so put only one condition i.e. either for sender or for reciever. Commented Sep 24, 2013 at 12:29
  • check if $userdata['id'] is sender then query with deleted_sender =0 vice versa for receiver Commented Sep 24, 2013 at 12:29

2 Answers 2

3

Well, think about what you're trying to do: you want to show it to the sender if the sender hasn't deleted it, and vice versa for the receiver.

In other words, you want to show if
(sender = user AND sender hasn't deleted) OR (receiver = user AND receiver hasn't deleted)

From this you build your query:

... WHERE (`sender_id`={$userdata['id']} AND `deleted_sender`=0)
       OR (`receiver_id`={$userdata['id']} AND `deleted_receiver`=0)
Sign up to request clarification or add additional context in comments.

Comments

0
    $r = mysql_query(
"SELECT * FROM private_messages 
WHERE (sender_id='".$userdata['id']."' AND deleted_sender='0') 
OR (receiver_id='".$userdata['id']."' AND deleted_receiver='0')"
);

Also this may end in invalid results since you don't know then if your result is about send or receive... depends on the display you like. But you may have to make two different requests.

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.