fife Posted March 28, 2012 Share Posted March 28, 2012 I have the most simple loop ever on this earth an its doin strange things. There are 6 rows in my cache table. I have a query to echo the rows onto the page with a loop. Now Im try to limit the rows returned to 1 row. simples eh. $FetchCacheq = mysql_query("SELECT * FROM cacheInfo LIMIT 1") or die('cache error'); <table width="480" border="0" cellpadding="0" cellspacing="5"> <tr> <td colspan="2" valign="baseline"><strong>Caches you have found</strong></td> </tr> <?php do { ?> <tr> <td width="250" height="31" align="left" valign="middle"><?php echo $row['cacheName'];?></td> <td width="230" align="left" valign="middle"><img src="/images/submit.png" width="20" height="20" /></td> </tr> <?php } while($row = mysql_fetch_assoc($FetchCacheq));?> </table> The problem I have is its showing two rows. Here is the source code from the page <table width="480" border="0" cellpadding="0" cellspacing="5"> <tr> <td colspan="2" valign="baseline"><strong>Caches you have found</strong></td> </tr> <tr> <td width="250" height="31" align="left" valign="middle"></td> <td width="230" align="left" valign="middle"><img src="/images/submit.png" width="20" height="20" /></td> </tr> <tr> <td width="250" height="31" align="left" valign="middle">Your Life in Their Hands</td> <td width="230" align="left" valign="middle"><img src="/images/submit.png" width="20" height="20" /></td> </tr> </table> as you can see one of the rows doesnt even have the cache name in it!!! please help im really confused. Quote Link to comment https://forums.phpfreaks.com/topic/259878-strange-looping-error/ Share on other sites More sharing options...
smerny Posted March 28, 2012 Share Posted March 28, 2012 instead of do...while($row = mysql_fetch_assoc($FetchCacheq)) use while($row = mysql_fetch_assoc($FetchCacheq)){ ... } Quote Link to comment https://forums.phpfreaks.com/topic/259878-strange-looping-error/#findComment-1331925 Share on other sites More sharing options...
PFMaBiSmAd Posted March 28, 2012 Share Posted March 28, 2012 do{}while() loops are almost never used because they require extra logic (which you don't have in your code) to set up the data before the start of the loop. For looping over database query results, where you don't expect exactly one row, you should always use a while(){} loop. Quote Link to comment https://forums.phpfreaks.com/topic/259878-strange-looping-error/#findComment-1331926 Share on other sites More sharing options...
scootstah Posted March 28, 2012 Share Posted March 28, 2012 If you're only going to have 1 row you don't need a while loop. Just do $row = mysql_fetch_assoc($FetchCacheq). Quote Link to comment https://forums.phpfreaks.com/topic/259878-strange-looping-error/#findComment-1331927 Share on other sites More sharing options...
fife Posted March 28, 2012 Author Share Posted March 28, 2012 no im actual echo rows based on a status number. I just used one for testing. PFMaBiSmAd can you explain why what you said fixed things. Id like to understand. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/259878-strange-looping-error/#findComment-1331931 Share on other sites More sharing options...
PFMaBiSmAd Posted March 28, 2012 Share Posted March 28, 2012 The post that smerny made shows using a while(){} loop. Quote Link to comment https://forums.phpfreaks.com/topic/259878-strange-looping-error/#findComment-1331932 Share on other sites More sharing options...
fife Posted March 28, 2012 Author Share Posted March 28, 2012 cool thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/259878-strange-looping-error/#findComment-1331933 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.