Table ID in PHP


Table ID in PHP

我试图为用PHP生成的表分配一个ID,但它一直返回一个错误。以下是迄今为止运行良好的完整代码。我只想在表中添加一个"id",这样我就可以在相关的工作表中对其应用css样式

        <?php
        $con=mysqli_connect("localhost","<un>","<pw>","monitor");
        // Check connection
        if (mysqli_connect_errno())
          {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
          }
        $result = mysqli_query($con,"SELECT * FROM presnationalresults ORDER BY Percentage DESC");
        echo "<table border='1'>
        <tr>
        <th>President</th>
        <th>Party</th>
        <th>Votes</th>
        <th>Percentage</th>
        </tr>";
        while($row = mysqli_fetch_array($result))
          {
          echo "<tr>";
          echo "<td>" . $row['PresidentName'] . "</td>";
          echo "<td>" . $row['PartyCode'] . "</td>";
          echo "<td>" . $row['Votes'] . "</td>";
          echo "<td>" . $row['Percentage'] . "</td>";
          }
        echo "</table>";
        mysqli_close($con);

有什么帮助吗?也许我的做法不对?

请尝试下面的代码块。我添加了表id,并在循环时关闭了内部的</tr>

<?php
$con = mysqli_connect("localhost", "<un>", "<pw>", "monitor");
// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM presnationalresults ORDER BY Percentage DESC");
echo "<table border='1' id='table-id'>
        <tr>
        <th>President</th>
        <th>Party</th>
        <th>Votes</th>
        <th>Percentage</th>
        </tr>";
while ($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['PresidentName'] . "</td>";
    echo "<td>" . $row['PartyCode'] . "</td>";
    echo "<td>" . $row['Votes'] . "</td>";
    echo "<td>" . $row['Percentage'] . "</td>";
    echo "</tr>"; // you forget close tr
}
echo "</table>";
mysqli_close($con);