PHP中的表ID

时间:2013-12-11 04:04:03

标签: php css

我正在尝试为我使用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);

有任何帮助吗?也许我会以错误的方式去做?

1 个答案:

答案 0 :(得分:0)

请尝试下面的代码块。我添加了表格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);