article

Sunday, May 2, 2010

PHP MYSQL - MySQL Average

PHP MYSQL - MySQL Average







MySQL Average
The AVG function returns the average value for the specified column of a group.



<?php
$con = mysql_connect("localhost","root","ednalan");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$query = "SELECT type, AVG(price) FROM products GROUP BY type"; //GROUP BY type
$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
echo "The average price of ". $row['type']. " is $".$row['AVG(price)'];
echo "
";
}

?>
//display
//The average price of Food is $8.730000

Related Post