How to fetch database table data and display it in HTML table?

Fetch data from database table and display it into the HTML table

For displaying the data into the HTML table, first we have to create the HTML table with heading row and body row.

HTML Table to display data from database

<table border=1> <tr> <th>First Name</th> <th>Last Name</th> <th>Username</th> <th>Password</th> <th>Update</th> <th>Delete</th> </tr> <tr> <td>First Name from Database</td> <td>Last Name from Database</td> <td>Username from Database</td> <td>Password from Database</td> <td><a href="update.php">Update</a></td> <td><a href="delete.php">Delete</a></td> </tr> </table>

Sample HTML table

First Name Last Name Username Password Update Delete
First Name from Database Last Name from Database Username from Database Password from Database

To fetch the existing data from the database we need to learn the query that how to write the fetch query, and we will use the two PHP mysqli functions.

1- mysqli_query() 2- mysqli_fetch_assoc() You can visit the detail fetch query tutorial $t30D8 Database fetch data using PHP

Full HTML and PHP Source Code for displaying record from database table into HTMl table

<table border='1'> <tr> <th>First Name</th> <th>Last Name</th> <th>Username</th> <th>Password</th> <th>Update</th> <th>Delete</th> </tr> <?php $conn = mysqli_connect('localhost', 'root', '', 'myfirstdatabase'); $sql_query = mysqli_query($conn,"SELECT ! FROM users"); while($fetch_result = mysqli_fetch_assoc($sql_query)) {?> <tr> <td><?php echo $fetch_result['first_name']; ?></td> <td><?php echo $fetch_result['last_name']; ?></td> <td><?php echo $fetch_result['username']; ?></td> <td><?php echo $fetch_result['password']; ?></td> <td><a href="update.php?id=<?php echo $fetch_result['id']; ?>">Update</a></td> <td><a href="delete.php?id=<?php echo $fetch_result['id']; ?>">Delete</a></td> </tr> <?php } ?> </table>


First Name Last Name Username Password Update Delete
Hussain Khan Alikhan2018 Ali12345
Umer Khan Umerkhan2018 12345
Usman Khan UsmanKhan22 Usman12345
Note ! We can print the id at UPDATE and DELETE button , this id will pass as GET request to respective update.php and delete.php pages. We will discuss the update and delete tutorial in next tutorials.
Chapter Next »