【问题标题】:How to run a While statement inside of a While statement to fetch values from a database?如何在 While 语句中运行 While 语句以从数据库中获取值?
【发布时间】:2011-06-14 23:34:22
【问题描述】:

这是我的功能:

        $result_display = mysql_query("SELECT * FROM Events ORDER BY TimeStamp DESC LIMIT $y, 20");
        $result_comment = mysql_query("SELECT * FROM Comments");


while($row_display = mysql_fetch_array($result_display)){
    echo "<div id='id'" . $row_display['ID'] . " class='eventdiv' data-sort='".$row_display['TimeStamp']."'>".$row_display['Title']." TIME: ".$row_display['TimeStamp']."</div>";

    while($row_comment = mysql_fetch_array($result_comment)){
        echo $row_comment['com_details'];
    }

}

HTML 输出类似于:

<div id='id12' class='eventdiv' data-sort='238781>Time: 238781</div>
<div id='id13' class='eventdiv' data-sort='238784>Time: 238784</div>
<div id='id14' class='eventdiv' data-sort='238785>Time: 238785</div>
<div id='id15' class='eventdiv' data-sort='238789>Time: 238789</div>
<div id='id16' class='eventdiv' data-sort='238791>Time: 238791</div>
<div id='id17' class='eventdiv' data-sort='238795>Time: 238795</div>

These are the comment details...

但我希望输出是这样的:

<div id='id12' class='eventdiv' data-sort='238781>Time: 238781</div>

These are the comment details...

<div id='id13' class='eventdiv' data-sort='238784>Time: 238784</div>

These are the comment details...

<div id='id14' class='eventdiv' data-sort='238785>Time: 238785</div>

These are the comment details...

<div id='id15' class='eventdiv' data-sort='238789>Time: 238789</div>

These are the comment details...

<div id='id16' class='eventdiv' data-sort='238791>Time: 238791</div>

These are the comment details...

<div id='id17' class='eventdiv' data-sort='238795>Time: 238795</div>

These are the comment details...

我认为我在 while 语句中使用 while 语句的方式会像上面的 HTML 一样显示它,但事实并非如此。

【问题讨论】:

    标签: php mysql phpmyadmin mysql-management


    【解决方案1】:

    在您第一次查看 $result_comment 结果后,您必须使用mysql_data_seek( $result_comment ) 重置它。所以:

    while($row_display = mysql_fetch_array($result_display)){
        echo "<div id='id'" . $row_display['ID'] . " class='eventdiv' data-sort='".$row_display['TimeStamp']."'>".$row_display['Title']." TIME: ".$row_display['TimeStamp']."</div>";
    
        while($row_comment = mysql_fetch_array($result_comment)){
            echo $row_comment['com_details'];
        }
    
        mysql_data_seek( $result_comment );
    
    }
    

    【讨论】:

      【解决方案2】:

      您需要对第一个 while 循环中的每个元素运行一个查询。目前,没有任何东西将 cmets 与每个事件相关联:

      $result_comment = mysql_query("SELECT * FROM Comments WHERE [id]=$id");

      【讨论】:

        【解决方案3】:

        @DemianBrecht,我将关联我尚未这样做的两个表。不需要协会就能得到我需要的东西—— @Jed,我不知道为什么,但你提出的解决方案会产生这个错误:

        'Wrong parameter count for mysql_data_seek()'
        

        我发现它为什么不起作用,第二个 fetch 函数需要放在第一个的 while 语句中。否则,它只会在完成后执行:

            $result_display = mysql_query("SELECT * FROM Events ORDER BY TimeStamp DESC LIMIT $y, 20");
        
        while($row_display = mysql_fetch_array($result_display)){
            echo "<div id='id'" . $row_display['ID'] . " class='eventdiv' data-sort='".$row_display['TimeStamp']."'>".$row_display['Title']." TIME: ".$row_display['TimeStamp']."</div>";
        
            while($row_comment = mysql_fetch_array($result_comment)){
        
               $result_comment = mysql_query("SELECT * FROM Comments");
               echo $row_comment['com_details'];
        
            }
        
        }
        

        足够简单。

        【讨论】:

        • 我强烈建议您使用我的解决方案,这样您就不会每次都对数据库进行新查询。也许您所需要的只是:mysql_data_seek( $result_comment, 0 )
        猜你喜欢
        • 2016-04-12
        • 2020-11-30
        • 2020-05-07
        • 2017-12-09
        • 2016-02-29
        • 2011-12-13
        • 2012-02-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多