【问题标题】:echo mysql data only once in while loop在while循环中只回显mysql数据一次
【发布时间】:2014-08-05 12:09:38
【问题描述】:

我已经设法进行了一个 php 聊天,其中每条消息在我的数据库表中都有一行,user_from 列当然会多次相同。现在我想回显发送给或从登录用户发送的最新消息。我只能通过使用此代码来回显每条消息。

 <?php

  $select = mysql_query("SELECT * FROM privat_mess WHERE userid_to='$idn' OR      
  userid='$idn' ORDER BY identitet DESC");

 while($row = mysql_fetch_assoc($select)){
     $id = $row['identitet'];
     $userid = $row['userid'];
     $userid_to = $row['userid_to'];
     $user_from = $row['user_from'];
     $user_to = $row['user_to'];
     $msg_body = $row['msg_body'];
     $opened = $row['opened'];


    if($userid == $idn) {
    echo '<div class>
        <p><strong>'.$user_to.'</strong></p>
            '.$msg_body.'
    </div>';
    }
else{
        echo '<div>
        <p><strong>'.$user_from.'</strong></p>
          '.$msg_body.'

    </div>';
}

}

?>

$userid 是用户表中发送消息的用户的 $id, $idn 是登录用户的 id。

我确信可以以某种方式使用 unique_array(),但如果有人能告诉我如何或是否有更好的方法,我将非常感激。我也尝试使用 DISTINCT,但无法正常工作。

我不希望它像 facebook 上的消息一样,其中一个顶部是最新的活动聊天。如果你按下它,你就会来到那个聊天室

编辑:

     $id = A_I, primary Key, the message id 
     $userid = the id for the user that sent the message 
     $userid_to = the id for the user that got the message 
     $user_from = name for the user who sent the message
     $user_to = name for the user who got the message
     $msg_body = The message text

我想显示每次聊天的最新消息。

我所说的一次聊天是指 $userid 和 $user_from 是唯一的。如果同一用户发送了更多消息,我不希望它们也显示出来。

【问题讨论】:

  • 一旦你想退出循环就使用break;
  • 注意:您正在使用的 mysql_* 函数已被弃用,并将在未来的 PHP 版本中删除。不要使用它们编写新代码,而是使用mysqli_* 或 PDO。
  • 只需将 LIMIT 1 添加到您的查询中,结果只会得到 1 行。
  • 好的,谢谢,杰拉德我会记住这一点。 JiFus,我试过了,但是我总共只收到一条消息,我想要每个聊天中的一条消息
  • 我想从每个聊天中得到一条消息你是什么意思?请向我们展示您的表结构

标签: php mysql while-loop


【解决方案1】:

根据您的评论但我希望每个用户发一条消息,而不是总共一条消息

试试这个:

SELECT 
     * 
FROM 
     privat_mess 
WHERE 
     userid_to='$idn' 
OR      
     userid='$idn' 
GROUP BY 
     userid
ORDER BY 
     identitet DESC

【讨论】:

  • @user3910137 那么您需要提供更多详细信息。告诉我表格结构和你想要的结果。编辑您的问题以添加更多详细信息
  • 看看帖子底部的EDIT,还有什么需要知道的吗?
【解决方案2】:
$select = mysql_query("SELECT * FROM privat_mess WHERE userid_to='$idn' OR      
                       userid='$idn' ORDER BY identitet DESC limit 0,1");

只会给你 1 条记录,第一条。我建议您阅读 limit 一两分钟,因为它可以大大加快您的查询速度!

编辑:

好吧,如果您只想要一个解决方案,而不是一个花哨的解决方案(感觉就像我下班回家时我的大脑离开了),只需取出您想要的标识,然后重新查询其中的每一个:

SELECT 
      max(identitet), userid_from
FROM 
     privat_mess 
WHERE
     userid_to='$idn' OR userid='$idn'
GROUP BY userid_from

您将获得每次聊天的最新身份,并且可以遍历每个聊天以获取有关每个聊天的更多信息。

如果你想从一开始就通过sql来解决,我有另一种方法,但它并不漂亮(不要在大型​​数据库上使用它!):

 SELECT * 
 FROM privat_mess 
 WHERE (userid_to='$idn' OR userid='$idn') AND 
 identitet IN (SELECT max(identitet) 
               FROM privat_mess
               WHERE userid_to='$idn' OR userid='$idn' 
               GROUP BY userid_from)

【讨论】:

  • 是的,但我希望每个用户发送一条消息,而不是总共一条消息。还是谢谢你:)
  • 好吧,从你的第一篇文章中看不到这一点! PS。为什么投反对票?我没有对原始问题发布错误的答案:/
  • 没用,我想如果我可以在表格中创建一个为聊天提供 ID 的列,我可以让它工作。但我不确定我是否能做到。
  • 我已经发布了解决方案,但我知道还有更漂亮的方法。不过,我似乎太累了,不能给你那些:/
猜你喜欢
  • 2015-09-28
  • 1970-01-01
  • 2016-08-21
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-23
  • 2011-11-02
相关资源
最近更新 更多