Home >Backend Development >PHP Tutorial >How to Display MySQL Database Table Data as an HTML Table on a Webpage?

How to Display MySQL Database Table Data as an HTML Table on a Webpage?

Susan Sarandon
Susan SarandonOriginal
2025-01-01 04:57:10741browse

How to Display MySQL Database Table Data as an HTML Table on a Webpage?

Show Values from MySQL Database Table in HTML Table on a Webpage

Querying the Database

To retrieve the values from a MySQL database table, you'll first need to connect to the database and execute a query. Consider the following PHP code:

$con = mysqli_connect("localhost", "database_user", "database_password", "database_name");
$result = mysqli_query($con, "SELECT * FROM tickets");

Here, we connect to the database using mysqli_connect and then execute the SELECT * FROM tickets query to fetch all the rows from the tickets table.

Displaying the Results in HTML Table

Once the results are retrieved, you can use HTML to create a table and display the values.

echo "<table border='1'>";
echo "<tr><th>Submission ID</th><th>Form ID</th><th>IP</th><th>Name</th><th>E-mail</th><th>Message</th></tr>";
while ($row = mysqli_fetch_array($result)) {
    echo "<tr><td>{$row['submission_id']}</td><td>{$row['formID']}</td><td>{$row['IP']}</td><td>{$row['name']}</td><td>{$row['email']}</td><td>{$row['message']}</td></tr>";
}
echo "</table>";

In this code, we start by creating a table with headers. Then, we use a while loop to iterate through the result and create a new row for each row in the table. The values from each row are obtained using the $row['column_name'] syntax.

Complete Code Example

Combining the query and display code, we get the following complete code:

This code will retrieve the values from the tickets table and display them in an HTML table on a webpage.

The above is the detailed content of How to Display MySQL Database Table Data as an HTML Table on a Webpage?. For more information, please follow other related articles on the PHP Chinese website!

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