Home > Database > Mysql Tutorial > How Can I Display MySQL Database Data in an HTML Table?

How Can I Display MySQL Database Data in an HTML Table?

Linda Hamilton
Release: 2024-12-31 22:34:14
Original
978 people have browsed it

How Can I Display MySQL Database Data in an HTML Table?

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:

  1. Establish Database Connection: Use the appropriate method to connect to the MySQL database.
  2. Execute Query: Execute an SQL query to retrieve the data from the "tickets" table.
  3. Fetch Results: Retrieve the data returned by the executed query.
  4. Create HTML Table: Create an HTML table with the appropriate headings corresponding to the database table columns.
  5. Populate HTML Table: Iterate over the fetched results and fill the HTML table rows with the corresponding data.

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);
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template