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 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人