我有以下php代码:
echo "<div style='float:left;'>";
echo "<table>";
echo "<tr>";
echo "<th></th>";
echo "<th colspan='4'>Laks beholdt</th>";
echo "</tr>";
echo "<tr>";
echo "<th>Uke</th>";
echo "<th><3 kg</th>";
echo "<th>3-7 kg</th>";
echo "<th>>7 kg</th>";
echo "<th>Totalt</th>";
echo "</tr>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) or die(mysql_error()))
{
echo "<tr>";
echo "<td>" . $row['Uke'] . "</td>";
echo "<td style='text-align:right; padding-right:10px;'>" . number_format($row['SumSmall'], 1,
",", " ") . " kg</td>";
echo "<td style='text-align:right; padding-right:10px;'>" . number_format($row['SumMedium'], 1,
",", " ") . " kg</td>";
echo "<td style='text-align:right; padding-right:10px;'>" . number_format($row['SumLarge'], 1,
",", " ") . " kg</td>";
echo "<td style='text-align:right; padding-right:10px;'>" . number_format($row['SumVekt'], 1, ",",
" ") . " kg</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";我从while循环中得到了预期的输出,但是我的表和div -or的结束标记并没有显示出来。我没有收到错误信息,并且在我的html中看不到任何错误。我尝试通过数字而不是关联来引用数组,但我得到了相同的结果。
我已经写了100个类似的循环,没有错误,但我在这里没有想法:/
发布于 2012-04-20 01:32:57
or die()语句导致它停止执行。当$row = mysql_fetch_array($result, MYSQL_ASSOC)应该停止循环时,它却命中die()。由于没有错误,因此不会从mysql_error()打印任何内容
echo "Before loop\n";
$x = 1;
while($foo = bar($x) or die('Died')) {
echo $x++, "\n";
}
echo "After loop\n";
function bar($x) {
if($x < 5) {
return $x;
}
return false;
}
//outputs:
//Before loop
//1
//2
//3
//4
//Died发布于 2012-04-20 01:32:41
不要在循环中使用die()
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<tr>";
echo "<td>" . $row['Uke'] . "</td>";
echo "<td style='text-align:right; padding-right:10px;'>" . number_format($row['SumSmall'], 1,
",", " ") . " kg</td>";
echo "<td style='text-align:right; padding-right:10px;'>" . number_format($row['SumMedium'], 1,
",", " ") . " kg</td>";
echo "<td style='text-align:right; padding-right:10px;'>" . number_format($row['SumLarge'], 1,
",", " ") . " kg</td>";
echo "<td style='text-align:right; padding-right:10px;'>" . number_format($row['SumVekt'], 1, ",",
" ") . " kg</td>";
echo "</tr>";
}发布于 2012-04-20 01:33:50
while条件是有缺陷的,因为当从结果集中获取所有行时,mysql_fetch_array()返回false,因此while条件变成了false or die(mysql_error()),它执行die()并终止脚本。
您不应该使用或喜欢那样,也不应该像那样将错误打印到屏幕上。
您正在执行的错误报告的类型应该取决于您是在生产环境中还是在开发环境中,但无论如何都应该执行错误检查。
我建议您强烈考虑像PDO with exceptions、mysqli或PHP framework这样的东西。使用mysql_*() API几乎总是一个坏主意。
https://stackoverflow.com/questions/10233474
复制相似问题