Jump to content

Problem with While loop


andrew_ww

Recommended Posts

I have a while loop that enables me to see all the records that a table contains, thr trouble is its not displaying the very lats record.  If I changed it a do loop it will display a blank record if no other exists.  Does anything look out of place with with code ?

 

<?php while ($row_rs_history = mysql_fetch_assoc($rs_history)) { ?>
<table width="450" align="center" cellpadding="1" cellspacing="1">
  <tr>
    <td width="50%"><div align="left"><?php echo $row_rs_history['issue']; ?></div></td>
    <td width="50%"><?php echo strftime('%d %B %Y', strtotime($row_rs_history['update_date'])); ?></td>
  </tr>
  <tr>
    <td colspan="2"><div align="left"></div>
      <div align="left"></div>      <div align="left">
        <hr />
      </div></td>
  </tr>
</table>
<?php } ?>

 

I've put the query string into MySQL Query Browser and it returns the correct number of results.

 

It's only when the query is run within the php page that I encounter problems.

 

I've placed the following code on to the page and this shows the correct number of results:

 

<?php echo $totalRows_rs_history; ?>

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/37270-problem-with-while-loop/
Share on other sites

<?php while ($row_rs_history == mysql_fetch_assoc($rs_history)) { ?>
<table width="450" align="center" cellpadding="1" cellspacing="1">
  <tr>
    <td width="50%"><div align="left"><?php echo $row_rs_history['issue']; ?></div></td>
    <td width="50%"><?php echo strftime('%d %B %Y', strtotime($row_rs_history['update_date'])); ?></td>
  </tr>
  <tr>
    <td colspan="2"><div align="left"></div>
      <div align="left"></div>      <div align="left">
        <hr />
      </div></td>
  </tr>
</table>
<?php } ?>

 

try it with 2 = signs

only one. that isnt really the answer, because you want to set $row_rs_history to equal (using one = sign) the array.

 

run in like this:

 

<?php $i = 0; while ($row_rs_history = mysql_fetch_assoc($rs_history)) { ?>
<table width="450" align="center" cellpadding="1" cellspacing="1">
  <tr>
    <td width="50%"><div align="left"><?php echo $row_rs_history['issue']; ?></div></td>
    <td width="50%"><?php echo strftime('%d %B %Y', strtotime($row_rs_history['update_date'])); ?></td>
  </tr>
  <tr>
    <td colspan="2"><div align="left"></div>
      <div align="left"></div>      <div align="left">
        <hr />
      </div></td>
  </tr>
</table>
<?php echo $i."<br>"; $i++; } ?>

 

and tell me how much times its being outputted to how much times it should be outputted.

 

I should be displaying two records:

 

$query_rs_history = sprintf("SELECT * FROM tbl_history WHERE site_id = %s ORDER BY history_id DESC", $colname_rs_history);
$rs_history = mysql_query($query_rs_history, $DII) or die(mysql_error());
$row_rs_history = mysql_fetch_assoc($rs_history);
$totalRows_rs_history = mysql_num_rows($rs_history);

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.