【发布时间】:2018-11-27 21:11:16
【问题描述】:
我正在尝试使用 SQL 和 PHP 编写清单,并尝试将 sql 数据库中的数据显示到 HTML 表中。我试图将它显示在两个不同的表中,一个显示已完成的记录,一个显示尚未完成的记录。 用于显示带有不完整记录的表的代码可以很好地显示它们,但第二个不显示记录。 任何帮助将不胜感激!
<div class="table_undone">
<!--Display undone checklist items-->
<h2>STILL TO DO</h2>
<?php
$sql = "SELECT * FROM checklist WHERE done = '' ORDER BY id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
//display the header of the table
echo "<table>
<tr>
<th>ID</th>
<th>ITEM</th>
<th>DUE DATE</th>
<th>DONE</th>
<th></th>
<th></th>
</tr>";
while ($row = $result->fetch_assoc()) {
//display the contents of the table
echo "<tr>
<td>".$row['id']."</td>
<td>".$row['item']."</td>
<td>".$row['due_date']."</td>
<td>".$row['done']."</td>
</tr>";
}
echo "</table>";
} else {
echo "0 RESULTS";
}
$conn->close();
?>
</div>
<div class="table_done">
<!--Display complete checklist items-->
<h2>COMPLETE!</h2>
<?php
$sql = "SELECT * FROM checklist WHERE done != '' ORDER BY id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
//display the header of the table
echo "<table>
<tr>
<th>ID</th>
<th>ITEM</th>
<th>DUE DATE</th>
<th>DONE</th>
<th></th>
<th></th>
</tr>";
while ($row = $result->fetch_assoc()) {
//display the contents of the table
echo "<tr>
<td>".$row['id']."</td>
<td>".$row['item']."</td>
<td>".$row['due_date']."</td>
<td>".$row['done']."</td>
</tr>";
}
echo "</table>";
} else {
echo "0 RESULTS";
echo $sql;
}
$conn->close();
?>
</div>
第二个表应该在哪里显示statemtn 0 RESULTS SHOWN,即使数据库中有应该显示的记录。
【问题讨论】:
-
使用
<>而不是!=。 -
done字段是如何定义的? -
在 mysql 中
!=和<>是等效的 @TheImpaler -
你确定 done 有空字段吗?不要与 NULL 混淆