<?php //connection $conn = new mysqli('localhost', 'root', '', 'dbtest'); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>How to Get the Average of One Column in MySQL Database using PHP</title> </head> <body> <!-- displaying our table --> <table border="1"> <thead> <th>ID</th> <th>Name</th> <th>Price</th> </thead> <tbody> <?php $sql = "SELECT * FROM products"; $query = $conn->query($sql); while($row = $query->fetch_assoc()){ echo " <tr> <td>".$row['id']."</td> <td>".$row['product_name']."</td> <td>".$row['product_price']."</td> </tr> "; } ?> </tbody> </table> <!-- getting the average --> <?php $sql = "SELECT AVG(product_price) AS average_price FROM products"; $query1 = $conn->query($sql); $row1 = $query1->fetch_assoc(); echo "Average Price: ".$row1['average_price']; ?> </body> </html>