PHP SQL Averages
Posted: Thu Jul 20, 2006 9:54 am
pickle | Please use
pickle | Please use[/syntax]
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi all,
Im struggling to extract the variables out of sql to find averages and sum.
I have the following code and I cannot seems to extract the values into variables.
Many Thanks in Advance
Jamie
[syntax="php"]
$sql = 'select count(*) as cnt, SUM(Score) as sum, (SUM(Score)/count(*)) as avg FROM tblChart;';
$sql = sql_return($sql);
$sql = mysql_query($sql);
$sql_result = @mysql_fetch_array($sql);
$avg=$sql_result['avg'];
$cnt=$sql_result['cnt'];
$sum=$sql_result['sum'];
//sql_return() is a function that I send all of my sql statements to and is displayed below
function sql_return($sql_text)
{
$conn = db_connect();
$sql = sql_text;
$query = $sql_text;
//echo $sql_text;
$results = @mysql_query($query,$conn);
$result = db_result_to_array($results);
return $result;
}
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]