Page 1 of 1

calculate average from rows in a database

Posted: Mon Jun 09, 2008 3:58 am
by abhikerl
hi, can anyone tell me how to calculate the average of data retrieves from a database. The situation is I have a table "number" which consist of several data like "2,5,4,8,6...." I want to calculate the average and display it on the browser. thanks in advance for your help.

http://www.islandinfo.mu

Re: calculate average from rows in a database

Posted: Mon Jun 09, 2008 4:17 am
by onion2k

Re: calculate average from rows in a database

Posted: Mon Jun 09, 2008 6:59 am
by madan koshti
Hi,

<?php
/* Connecting, selecting database */
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die("Could not connect : " . mysql_error());
echo "Connected successfully";
mysql_select_db("my_database") or die("Could not select database");

/* Performing SQL query */
$query = "SELECT AVG("filedname") as avg FROM numers";
$result = mysql_query($query) or die("Query failed : " . mysql_error());

$line = mysql_fetch_array($result, MYSQL_ASSOC)) ;
echo "$line['avg']\n";

/* Free resultset */
mysql_free_result($result);

/* Closing connection */
mysql_close();

?>

hope that was needed ???