I’m looping a table with mySQL and with a while function and the table builds all the table headers and the loop, but nothing under the loop. I have a <?php endwhile ?> after the loop but it won’t display anything under that. It will echo more than one tr from the database but I loose the rest of the page!
<?php
$inttotalcredits = 0;
$result = mysql_query("SELECT * FROM site_products JOIN site_trans ON site_products.product_id = site_trans.trans_product");
while($row = mysql_fetch_array($result) or die(mysql_error())) :
?>
<tr>
<td><center><?php echo $row['product_id'] ?></center></td>
<td><center><?php echo $row['product_name'] ?></center></td>
<td><center><?php echo $row['product_number'] ?></center></td>
<td><center><?php echo $row['product_description'] ?></center></td>
<td><center><?php echo $row['trans_inventory'] ?></center></td>
<?php
$inttotalcredits += $row['trans_inventory']; ?>
</tr>
<?php endwhile ?>
</table>
<center><?php echo $inttotalcredits ?></center>
</p><p><a href="logout.php">Logout</a></p>
Don’t call
die()while fetching results.mysql_fetch_array()returns false when you reach the end of the result set an no more results are available, which triggers yourdie()call and cancels execution of the rest of your script.It is recommended (and an important habit to get into) to wrap your database output in
htmlspecialchars()to encode any<>&etc that they may include, but also to protect against cross-site scripting attacks when displaying user-input.