HTML table 使用 MySQLi 和 PHP

HTML table using MySQLi and PHP

我正在尝试 table 连接到我的数据库,以便我可以使用该数据库中的信息。

我认为我的方法是正确的,但目前 table 只显示第一行。有人对发生的事情有任何想法吗?

         <table id="fairtable">
            <tr>
                <td>Fair Name</td>
                <td>Date</td>
                <td>Are we there?</td>
                <td>Website</td>
            </tr>
            <?php 
            $sql = "SELECT `name`,`date`,`present`,`website` FROM `dates`";
            $results = mysqli_query($conn,$sql);

            while($rowitem = mysqli_fetch_array($results)) {
            echo "<tr>";
                echo "<td>" . $rowitem['name'] . "</td>";
                echo "<td>" . $rowitem['date'] . "</td>";
                echo "<td>" . $rowitem['present'] . "</td>";
                echo "<td>" . $rowitem['website'] . "</td>";
            echo "</tr>";
            }
            ?>
        </table>

您的代码仅从 table 获取一行,您需要循环结果。
试试这个代码:

$results = mysqli_query($conn,$sql);

while($row = mysqli_fetch_array($results))
{
  //do smth
}

这可能是因为您没有正确打开HTML table标签,这很容易被忽视。同样在白色语句中循环结果集:

$sql = "SELECT `name`,`date`,`present`,`website` FROM `dates`";
$results = mysqli_query($conn,$sql);
echo "<table>"; //begin table tag...
//you can add thead tag here if you want your table to have column headers
 while($rowitem = mysqli_fetch_array($results)) {
    echo "<tr>";
    echo "<td>" . $rowitem['name'] . "</td>";
    echo "<td>" . $rowitem['date'] . "</td>";
    echo "<td>" . $rowitem['present'] . "</td>";
    echo "<td>" . $rowitem['website'] . "</td>";*/
    echo "</tr>";
}
echo "</table>"; //end table tag