while循环后输出停止,不使用“或die()”

时间:2013-02-27 22:15:57

标签: php loops while-loop

我知道同一主题还有另一个问题,但该问题涉及or die()语句。我有同样的问题,但我没有在我的while循环中使用或die()语句。答案可能很明显,但我无法弄清楚。

$Dates = "1";
$Select = $mysqli->query("SELECT * FROM ----");
while ($Dates != "") {
$Dates = $Select->fetch_assoc();
$Get = $mysqli->query("SELECT * FROM " . $Dates["Meeting_Date"] . " WHERE Last_Name='" . $LName . "'");
$Vals = $Get->fetch_assoc();
if ($Vals != "") {
$TimeIn = $Vals["Time_In"];
$TimeIn2 = explode(":",$TimeIn);
$TotIn = ($TimeIn2[0]*60)+$TimeIn2[1];
$TimeOut = $Vals["Time_Out"];
$TimeOut2 = explode(":",$TimeOut);
$TotOut = ($TimeOut2[0]*60)+$TimeOut2[1];
$TotTime = ($TotOut - $TotIn)/60;
$TotalTime = floor($TotTime) . " Hours " . ($TotTime-floor($TotTime))*60 . " Minutes";
echo "<tr><td>" . str_replace("_","/",$Dates["Meeting_Date"]) . "</td><td>" . $TimeIn . "</td><td>" . $TimeOut . "</td><td>" . $TotalTime . "</td></tr>";
}
if ($Vals == "") {
echo "<tr><td>" . str_replace("_","/",$Dates["Meeting_Date"]) . "</td><td colspan='3' style='background-color: rgba(0,0,0,0.25)'>Did not Attend</td></tr>";
}
}

我在while循环之后放的任何内容都没有被执行,即使while循环只执行2次就像它应该... ...

赞赏任何想法

1 个答案:

答案 0 :(得分:1)

这是问题所在:

while ($Dates != "") {
  $Dates = $Select->fetch_assoc();
  $Get = $mysqli->query("SELECT * FROM " . $Dates["Meeting_Date"] . " WHERE Last_Name='" . $LName . "'");

当没有更多记录时,$Dates将为空(NULL),因此下一行将导致致命错误。

您可以将其更改为:

while ($Dates = $Select->fetch_assoc()) {
  $Get = $mysqli->query("SELECT * FROM " . $Dates["Meeting_Date"] . " WHERE Last_Name='" . $LName . "'");