Displaying MySQL Database Values in an HTML Table
This question aims to retrieve data from a MySQL database table and present it in an HTML table on a webpage. Despite searching for this basic database functionality, the asker was unable to find a solution. The database table, named "tickets," contains six fields: submission_id, formID, IP, name, email, and message. The asker desires to display the data in a table similar to the provided HTML markup.
Solution
To display the database values in an HTML table, follow these steps:
Here's an example code that demonstrates this solution:
$con = mysqli_connect("localhost", "username", "password", "database_name"); $result = mysqli_query($con, "SELECT * FROM tickets"); $data = $result->fetch_all(MYSQLI_ASSOC); ?> <table border="1"> <tr> <th>Submission ID</th> <th>Form ID</th> <th>IP</th> <th>Name</th> <th>Email</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> <?php mysqli_close($con);
This code establishes a database connection, executes a query to retrieve data from the "tickets" table, and retrieves the results. It then creates an HTML table and iterates through the data to populate the table rows. Finally, it closes the database connection.
The above is the detailed content of How Can I Display MySQL Database Data in an HTML Table?. For more information, please follow other related articles on the PHP Chinese website!