Displaying MySQL Database Values in an HTML Table on a Webpage
To retrieve and display values from a database table in an HTML table, follow these steps:
Get the Data:
Example:
$con = mysqli_connect("localhost", "peter", "abc123", "my_db"); $result = mysqli_query($con, "SELECT * FROM tickets LIMIT 50"); $data = $result->fetch_all(MYSQLI_ASSOC);
Display the Data:
Example:
<table border="1"> <tr> <th>Submission ID</th> <th>Form ID</th> <th>IP</th> <th>Name</th> <th>E-mail</th> <th>Message</th> </tr> <?php foreach($data as $row): ?> <tr> <td><?= htmlspecialchars($row['submission_id']) ?></td> <td><?= htmlspecialchars($row['formID']) ?></td> <td><?= htmlspecialchars($row['IP']) ?></td> <td><?= htmlspecialchars($row['name']) ?></td> <td><?= htmlspecialchars($row['email']) ?></td> <td><?= htmlspecialchars($row['message']) ?></td> </tr> <?php endforeach ?> </table>
This code will generate an HTML table with the data from the "tickets" table, displaying the values in the corresponding columns.
The above is the detailed content of How to Display MySQL Database Values in an HTML Table?. For more information, please follow other related articles on the PHP Chinese website!