weixin_33734785 2017-04-18 07:45 采纳率: 0%
浏览 20

一对一聊天PHP

I am trying to develop a one to one chat. I want the users to only be able to see messages from the sender i.e the sender only sees the messages from himself and the receive but the query fails to run. I am able to see all the messages present in the database

public function getMessages() {

 $messages = array();
 $query = <<<QUERY
    SELECT 
      `chat`.`message`, 
      `chat`.`sent_on`,
      `user1`.`id`, 
      `user1`.`first_name`
    FROM `user1`
    JOIN `chat`
      ON `chat`.`user_id` = `user1`.`id` AND `chat`.`rec_id`=$recid
    ORDER BY `sent_on`
  QUERY;
  • 写回答

1条回答 默认 最新

  • weixin_33716557 2017-04-18 07:51
    关注

    You need to specify userId in your query like this:

    public function getMessages($userId) {
    
      $messages = array();
      $query = <<<QUERY
         SELECT 
           `chat`.`message`, 
           `chat`.`sent_on`,
           `user1`.`id`, 
           `user1`.`first_name`
         FROM `user1`
         JOIN `chat`
           ON `chat`.`user_id` = `user1`.`id` AND `chat`.`rec_id`=$recid
         WHERE `user1`.`id` = {$userId}
         ORDER BY `sent_on`
       QUERY;
    
    评论

报告相同问题?