Jump to content

strange looping error


fife

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/259878-strange-looping-error/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.