Group Messaging in php mysql
<p>I have created a table in MySQL with following columns:</p>

<p><code>id - from_id - to_id - datetime - message</code></p>

<p>My PHP code is :</p>

<pre><code>$user = $_SESSION['user'];

$sql= "SELECT * FROM messages where to_id=".$user;

$result = $conn-&gt;query($sql);

echo "&lt;table&gt;&lt;tr&gt;&lt;td&gt;FROM&lt;/td&gt;&lt;td&gt;MESSAGE&lt;/td&gt;&lt;/tr&gt;";

while($row=mysqli_fetch_assoc($result)){
    $from = $row['from_id'];
    $message = $row['message'];

    if(count($from) &gt;1){
        echo "More than 1 message from ".$from;
    }

    echo "&lt;tr&gt;&lt;td&gt;".$from."&lt;/td&gt;&lt;td&gt;".$message."&lt;/td&gt;&lt;/tr&gt;";

}
echo "&lt;/table&gt;";
</code></pre>

<p>I can get all the messages sent to the user. But Im also getting messages sent FROM the same user in different lines. So for example, if user2 has sent user1 4 messages, all the 4 messages are listed in this list. I want only one row for the user2 and when i click on the row, i will see all the messages from that user. But on this page i want to see only 1 row with the latest message.</p>

<p>How can I achieve this?</p>