MySQL Avg – Using this average built in function in PHP and Database


This is an important beginner tutorial on MySQL avg. For those needing tips on how to properly syntax MySQL query to compute average of values stored in the database. Here it is:

$result3 = mysql_query("SELECT avg(close) from `sp500` WHERE `entry`>='$lowerlimit' AND `entry`<='$upperlimit'")
or die(mysql_error());
$row3 = mysql_fetch_array($result3)
or die("Invalid query: " . mysql_error());
// Print out the contents of the entry
$ma200 = $row3['avg(close)'];

Of course you can use your own PHP variable. The above script will compute the average of values of “close” field in the “sp500″ database defined by upper and lower limit. And the resulting average value is then assigned to a PHP variable $ma200.

I have made a complete script on this, which is featured in this project S&P 500 technical analysis using moving average and this is the script.



Related posts: