How to display MySQL data in HTML table using Php


The most attractive and best way to display the database records in table format. Below is the simple example to display the records stored in MySQL table.
Record Table of Employee
NAME
DEVICE_NAME
NUMBER_OF_UNITS SOLD 
TOTAL_PRICE
ROHAN
OVEN 
10
70300
SHAYAM
LAPTOP
8
305000
KAMRAN
CAMERA 
15
10200
NATHAN
CELL PHONE 
20
80000
MARAYA
TELEVISION
5
60000
The above table is a sample example of MySQL table with some records of data.

We need to fetch these data in HTML table to view the data on browser.

<?php
@mysql_connect('localhost','root','') or die('check username and password');
@mysql_select_db('record') or die('not connected');
?>
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
<style type="text/css">
table
{
 width:50em;
}

fieldset
{
 width:20em;
 background-color:lightgreen;
}
</style>
</head>
<body bgcolor="lavender">
<div>
<center>
<fieldset>
<legend><font size="5px" color="blue">Record of Employee</font></legend>
<table border="1" rules="rows">
<tr bgcolor="red"><th>NAME</th><th>DEVICE NAME</th><th>NUMBER OF UNITS</th><th>TOTAL AMOUNT</th></tr>
<?php
$query = mysql_query("select * from employee");
while($result = mysql_fetch_array($query))

?>
<tr align="center"><td><?php echo $result['name']; ?> </td><td><?php echo $result['device_name']; ?> </td><td><?php echo $result['number_of_units']; ?> </td><td><?php echo $result['total_price']; ?> </td></tr>
<?php } ?>
</table> 
</fieldset>
</center>
</div>
</body>
</html>

Output of the above



No comments: