Below is the code and you will need to rename the username and date columns in the comments table to _username and _date
<?php
//Insert mysql connections here
$r=mysql_query('SELECT * FROM `comments`, `messages` WHERE `comments`.`msg_id_fk` = `messages`.`msg_id` ORDER BY `comments`.`com_id`, `messages`.`msg_id`') or die(mysql_error());
$messagedump='';
while ($row = mysql_fetch_assoc($r)) {
if ($messagedump!=$row['message']) {
echo <<< HTML
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>{$row['username']}</td>
<td>{$row['message']}</td>
</tr>
<tr>
<td>{$row['date']}</td>
<td>nbsp;</td>
</tr>
</table></td>
</tr>
HTML;
}
echo <<< HTML
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>{$row['_username']}</td>
<td>{$row['comment']}</td>
</tr>
<tr>
<td>{$row['_date']}</td>
<td> </td>
</tr>
</table></td>
</tr>
</table>
HTML;
}
?>