Php mysql结果进入无限循环

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>';
        }
    }

我有sql查询结果存储在数据库中。我必须检查是否有任何行包含字符串'N;' 然后下一个sql语句应该执行。我有很多这样的字符串,这就是为什么要执行我的下一个sql语句。我试过以下代码但是当我尝试运行代码时它会进入无限循环。感谢你。 这是我的PHP 代码: p>

  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。='&lt; table id =“news”&gt;  
&lt; a href =“news.php?id =”&gt; 
&lt; p class =“welcome-subheadline”&gt;  ”。  $ config。'&lt; / p&gt;&lt; / a&gt;  
&lt; / table&gt;'; 
} 
} 
  code>  pre> 
  div>

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>';
            }
        }

You're using the $result in your loop condition and you're also redefining it inside your loop. The behaviour here is likely to be undefined. Look at it this way (in pseudocode):

$result = perform some query

while($result has some rows) {
    do some stuff

    $result = perform some other query
}

The while route is using that first $result value to keep track of where it is and what it's doing. But inside the loop you're replacing that with the results of some new query. So basically, what's happening is that the while loop cannot keep track of where it is resulting in (in your case) an infinite loop.

The solution is to rewrite your loop so that you're not destroying the values that PHP is using to keep track. How you do that will likely depend on the code around your loop as much as the loop itself, so I'm not sure what's best. Here's one solution (again in pseudocode):

$keepLooping = true

while($keepLooping) {
    $result = perform some query

    do some stuff

    if ($result meets some exit condition) {
        $keepLooping = false;
    }
}