简体   繁体   中英

inbox sql query for messaging system

Am building a message system with php and mysql and am using this tutorial as my guide http://aaronsaray.com/blog/2010/07/27/facebook-message-system-in-php

Am working on the inbox page and am having issues with the inbox as it wont select messages that have already been read, so when a user opens their inbox to find prior message, they are presented with a blank page because the query wont select their messages which they have read.

this is the query

   $sql =  "select m.mid, m.seq, m.created_on, m.created_by, m.body, r.status from message_recips r
            inner join ".$this->msg_table." m on m.mid=r.mid and m.seq=r.seq 
            where r.uid=".$this->user_id." and r.status in ('A', 'N')
            and r.seq=(select max(rr.seq) from message_recips rr where rr.mid=m.mid and rr.status in ('A', 'N'))
            and if (m.seq=1 and m.created_by=".$this->user_id.", 1=0, 1=1)
            order by m.created_on desc";

This is explanation of the query from the tutorial

First thing is to get both of the identifiers for the message (MID/SEQ), when it was created (so we can show the date), who created it (so we can show the originator or who it is 'from'), and the status. The status will just be used to show if that message is new.

The sql gets the data from the recips table first. This is the pointer to all of the 'copies' of the initial message that should be available. Note that the message table itself is joined on so we can get the actual content of the message. Next, the recipient UID is verified to be the current user and the message must be either New or Active. Next, the sequence number must be a specific one. In this case a subselect is done. The maximum sequence number (so that would make it the newest) from the recips table where that message is the current message and the status is not deleted. In this case we don't verify that the UID of that subselect is any user because we want to show any originator whether it be ourself or someone else. The last part of the where clause verifies that the sequence number is not 1 and that its not created by our current user. If it is 1, that means its the first message of the thread, created by us, and that we shouldn't select it. Your inbox never shows items that you have originally sent but received no responses.

Then, the rest is pretty simple. All of the items are retrieved. A loop is generated and each 'newest' message is shown with a link to view it. Notice how the view link only has the MID, however. We don't need to know the sequence number as we'll be showing the entire thread.

How should this query be modified?

Using the website you provided:

 we have a STATUS column – which will be N for new, A for active (or read) and D for deleted.

In your SQL query:

and r.status in ('A', 'N')

This means it will select both (N)ew and (A)ctive/Read messages. If you wanted to show ONLY active/read messages, change that above line to read:

and r.status='A'

If you are still having problems, check out your MySQL table and be sure the message statuses are being updated to 'A' after they are read and not being deleted or marked differently.

Also, just to troubleshoot, echo out your $sql and see that there are no spaces in $this->msg_table and that your $this->user_id is displaying properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM