获取用户的最新消息

时间:2012-08-11 18:37:49

标签: php sql

如何从向用户发送消息的人那里获取最新消息?

现在它正在显示......

first_name last_name       This is the newest message I sent you   2012-08-11 14:23:38

first_name last_name       test                                    2012-08-11 14:20:35

<?php 
    echo '<div id ="inbox_name">Name</div><div id="inbox_message">Message</div><div id="inbox_time">Time</div><div id="inbox_line"></div>';
    $query = mysql_query("SELECT * FROM `private_messages` WHERE `to_id`=$session_user_id ORDER BY `time_sent` DESC") or die("Error connecting to database. Please try again later.");
     while ($row = mysql_fetch_array($query)) {
        $to_id = $row['to_id'];
        $from_id = $row['from_id'];
        $message = $row['message'];
        $time= $row['time_sent'];

        $message_information = mysql_query("SELECT * FROM `sentrl_users` WHERE `id`=$from_id LIMIT 1");
        while ($row_information = mysql_fetch_array($message_information)) {
            $first_name = $row_information['first_name'];
            $last_name = $row_information['last_name'];
            $username = $row_information['username'];
            echo '<div id="inbox_information"><div id="recieved_name">'. $first_name . ' ' . $last_name .'</div><div id="recieved_message">'. $message .'</div><div id="recieved_time">'. $time .'</div></div>';
        }
    }
    ?>

我尝试使用限制1,但是没有用。我正在尝试获取最新消息而不重复first_namelast_name

1 个答案:

答案 0 :(得分:1)

如果您想要的只有一行,而不是

while ($row = mysql_fetch_array($query)) {
//EVERYTHING INSIDE THE ORIGINAL WHILE LOOP
} 

你应该写

while($row=mysql_fetch_array($query)){
$array[$row["from_id"]]=$row;
} 
foreach($array as $row){
//EVERYTHING INSIDE THE ORIGINAL WHILE LOOP
}