0

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>';
        }
    }
3
  • So long as your inner query ever has a single result, this will always result in an infite loop. You should consider changing the architecture of this. What is it you are trying to accomplish big-picture? Commented Jun 25, 2014 at 15:32
  • ohh i mean how can i change the architectue ? i made simple statements if i found a string then it should run sql statement tht's it but unable to do so :( Commented Jun 25, 2014 at 15:38
  • The first sql ( before the while loop ) is different from the one that is in the loop? Commented Jun 25, 2014 at 16:27

2 Answers 2

2

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;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I am sorry but you answer is confusing...should i write this while($keeplooping) inside while ($row = mysql_fetch_assoc($result)) ? because i already loop executing..
This is intended to replace your loop. But as I mentioned (and as @cale_b pointed out in his comment to your question), the way you write this is going to depend a lot on what you're trying to do, so I don't know if the way I've written it is the best way for you to do it. The key is to stop modifying the variable you're using as a condition for your loop inside your loop.
when i say if($config==Null) then everything is working fine but i have row which is having value 'N;' and i want to replce it from my next sql statement results....
1

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 Comment

I'm glad that I could help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.