PHP mysql select average program example

PHP mysql select average program example

PHP MySQL code to to calculate average.

 

<!doctype html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<title>Example of  avg function in php MySQL – Blog.eduguru.in</title>
<meta name=”description” content=”Example of  avg function in php MySQL”>
<link rel=”stylesheet” href=”https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css”>
</head>
<body>
<div class=”container”>
<div class=”row”>
<div class=”col-md-12″>
<h2>Example to show number of pages in average from a collection of books:</h2>
<table class=’table table-bordered’>
<tr>
<th>Average number of pages</th>
</tr>
<?php
$hostname=”your_hostname”;
$username=”your_username”;
$password=”your_password”;
$db = “your_dbname”;
$dbh = new PDO(“MySQL:host=$hostname;dbname=$db”, $username, $password);
foreach($dbh->query(‘SELECT AVG(no_page)
FROM book_mast’) as $row) {
echo “<tr>”;
echo “<td>” . $row[‘AVG(no_page)’] . “</td>”;
echo “</tr>”;
}
?>
</table>
</div>
</div>
</div>
</body>
</html>