dpt8910 2014-06-25 15:31
浏览 39
已采纳

Php mysql结果进入无限循环

Hi i have sql query result stored in the database.I have to check if any row contains string 'N;' then the next sql statement should execute.I have many such strings thats why want to execute my next sql statement.I have tried following code but when i try to run the code it goes to infinite loop.Thank you. Here is my php code:

while ($row = mysql_fetch_assoc($result)) {
        $config= $row['configuration'];     
        if($config=='N;')
        {
            $sql="SELECT DISTINCT ad_news_texte.id, ad_news_texte.headline, ad_news.datum_archiv FROM autodo.ad_news_texte INNER JOIN autodo.ad_news_oe ON ad_news_texte.news_id = ad_news_oe.id_ad_news INNER JOIN autodo.ad_news ON ad_news_oe.id_ad_news = ad_news.id WHERE ad_news.datum_archiv BETWEEN curdate( ) - INTERVAL DAYOFWEEK( curdate( ) ) +28 DAY AND curdate( )";
            $result = mysql_query($query, $myConnection);
        }
        else{                               
            $html .= '<table id="news">                     
                <a href="news.php?id=">
                <p class="welcome-subheadline"> '. $config .'</p></a>                   
                </table>';
        }
    }
  • 写回答

2条回答 默认 最新

  • dongyi6183 2014-06-25 16:50
    关注

    Ok, so I miss some information, but I will try to solve your problem either way:

    In the case if your sql fetch is identical to your sql statement in the loop, than you code should look like this: ( In this case I do not see the reason for the if statement )

    $sql="SELECT DISTINCT ad_news_texte.id, ad_news_texte.headline, ad_news.datum_archiv FROM autodo.ad_news_texte INNER JOIN autodo.ad_news_oe ON ad_news_texte.news_id = ad_news_oe.id_ad_news INNER JOIN autodo.ad_news ON ad_news_oe.id_ad_news = ad_news.id WHERE ad_news.datum_archiv BETWEEN curdate( ) - INTERVAL DAYOFWEEK( curdate( ) ) +28 DAY AND curdate( )";
    $result = mysql_query($sql, $myConnection);
    
    while ($row = mysql_fetch_assoc($result)) {
            $config= $row['configuration'];     
            if($config=='N;')
            {
                // If the sql is the same no need for another fetch
            }
            else{                               
                $html .= '<table id="news">                     
                    <a href="news.php?id=">
                    <p class="welcome-subheadline"> '. $config .'</p></a>                   
                    </table>';
            }
        }
    

    In the second case, when the sql statements are differ, than your code should look like this:

    $result = mysql_query($sql, $myConnection);
    while ($row = mysql_fetch_assoc($result)) {
            $config= $row['configuration'];     
            if($config=='N;')
            {
               $sql2="SELECT DISTINCT ad_news_texte.id, ad_news_texte.headline, ad_news.datum_archiv FROM autodo.ad_news_texte INNER JOIN autodo.ad_news_oe ON ad_news_texte.news_id = ad_news_oe.id_ad_news INNER JOIN autodo.ad_news ON ad_news_oe.id_ad_news = ad_news.id WHERE ad_news.datum_archiv BETWEEN curdate( ) - INTERVAL DAYOFWEEK( curdate( ) ) +28 DAY AND curdate( )";
               $result2 = mysql_query($sql2, $myConnection);
               while ($row2 = mysql_fetch_assoc($result2)) {
                   //The rest of the code
               }
    
             }
                else{                               
                    $html .= '<table id="news">                     
                        <a href="news.php?id=">
                        <p class="welcome-subheadline"> '. $config .'</p></a>                   
                        </table>';
                }
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大