【问题标题】:Why is my second select sql comman not working when the first one has?为什么我的第二个 select sql 命令在第一个有时不起作用?
【发布时间】: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,即使数据库中有应该显示的记录。

【问题讨论】:

  • 使用&lt;&gt; 而不是!=
  • done 字段是如何定义的?
  • 在 mysql 中 !=&lt;&gt; 是等效的 @TheImpaler
  • 你确定 done 有空字段吗?不要与 NULL 混淆

标签: php html sql select


【解决方案1】:

如果我没看错,你在第一个之后关闭了连接:

$conn->close();

所以第二个查询没有连接(它使用相同的 $conn 已关闭)。在第一次查询后(最后)删除该行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多