与MySQL混淆的循环

时间:2010-10-11 14:06:59

标签: php mysql loops

我正在写一个简单的留言板,你可以回复任何线程,然后回复任何回复等等......一切正常但有一个简单的方法来循环查询,因为这可能继续

    $rsql = "SELECT * FROM rotd_mb WHERE reply='N' ORDER BY dateTime DESC";
$result = runSQL($rsql);
while ($row = mysql_fetch_array($result)) {
echo "".$row[title]."";
    $rsql2 = "SELECT * FROM rotd_mb WHERE reply='Y' AND replyID='$row[messageID]' ORDER BY dateTime DESC";
    $result2 = runSQL($rsql2);
    while ($row2 = mysql_fetch_array($result2)) {
    echo "".$row2[title]."";
        $rsql3 = "SELECT * FROM rotd_mb WHERE reply='Y' AND replyID='$row2[messageID]' ORDER BY dateTime DESC";
        $result3 = runSQL($rsql2);
        while ($row3 = mysql_fetch_array($result3)) {
        echo "".$row3[title]."";
        }
        mysql_free_result($result3);    
    }
    mysql_free_result($result2);    
}
mysql_free_result($result);

1 个答案:

答案 0 :(得分:0)

你可以试试UNION,可能不是最好的解决方案,但它会起作用

$sql = "SELECT * FROM rotd_mb WHERE reply='N' ORDER BY dateTime DESC
        UNION
        SELECT * FROM rotd_mb WHERE reply='Y' AND replyID='$row[messageID] ORDER BY dateTime DESC
        UNION
        SELECT * FROM rotd_mb WHERE reply='Y' AND replyID='$row2[messageID]' ORDER BY dateTime DESC";

更好的想法是重新设计数据库表结构。