Reputation: 139
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>';
}
}
Upvotes: 0
Views: 1412
Reputation: 317
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>';
}
}
Upvotes: 1
Reputation: 15750
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;
}
}
Upvotes: 2