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!