【问题标题】:Nothing showing under PHP and mySQL <tr> [closed]PHP 和 mySQL <tr> 下没有显示 [关闭]
【发布时间】:2012-01-16 20:16:05
【问题描述】:

我正在使用 mySQL 和 while 函数循环一个表,该表构建了所有表头和循环,但循环下没有任何内容。我在循环之后有一个&lt;?php endwhile ?&gt;,但它不会显示任何内容。它会从数据库中回显不止一个 tr,但我会丢失页面的其余部分!

<?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>

【问题讨论】:

  • 向我们展示循环本身的代码。
  • 对不起No code, no help!
  • 我们帮不了你,因为你没有在这里放任何源代码。阅读常见问题解答:stackoverflow.com/faq
  • 这是我见过的最同步的“向我们展示你的代码”cmets。

标签: php mysql while-loop html-table


【解决方案1】:

在获取结果时不要调用die()mysql_fetch_array() 当您到达结果集的末尾时返回 false 并且没有更多可用的结果,这会触发您的 die() 调用并取消脚本其余部分的执行。

// Don't do this:
while($row = mysql_fetch_array($result) or die(mysql_error()))

// Instead do this:
while($row = mysql_fetch_array($result))

建议(并且养成一个重要的习惯)将您的数据库输出包装在 htmlspecialchars() 中以编码它们可能包含的任何 &lt;&gt;&amp; 等,但也可以在显示用户时防止跨站点脚本攻击 -输入。

<?php echo htmlspecialchars($row['product_name']); ?>

【讨论】:

  • 谢谢,我对 mysql 还很陌生,所以这是一个巨大的帮助!我曾在其他地方问过,他们建议添加 or die 来查找错误 - 不要再问了。
  • @OliverWhysall 使用or die() 习语通常用于测试连接是否成功或查询是否成功,但在获取结果时无法使用。
猜你喜欢
  • 1970-01-01
  • 2020-11-17
  • 2014-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多