HTML table using MySQLi and PHP

2019-03-05 02:45发布

问题:

I'm trying to make a table connected to my database so I can use the information in that database.

I think I'm on the right way, but currently the table only displays the first row. Anyone got any ideas on what's going on?

         <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>

回答1:

Perhaps this is because you have not opened your HTML table tags correctly, its an easy thing to overlook. Also loop the result set inside a white statement:

$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


回答2:

Your code gets only one row from table, you need loop your results.
Try this code:

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

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


标签: php mysqli