Jump to content

Adding Up Numbers in Mysql Table


gladiator83x

Recommended Posts

Hi All,

I was just wondering if someone could assist me in adding up a column full of numbers in my mysql table? I am trying to compute the average. This is what I started off with--but it doesnt return the sum. My table's name is More_Metrics. The column within my table is named Days.

$query= "SELECT More_Metrics.Days FROM More_Metrics WHERE More_Metrics.Topic_Title='$title'";
$result1 = mysql_query($query) or die('Query failed: ' . mysql_error());
$i=0;
while ($line = mysql_fetch_array($result1, MYSQL_ASSOC)) {
echo "<tr>\n";


  foreach ($line as $col_value1) {
//   echo "\t\t<td>$col_value1</td>\n";

$i=$col_value1 +$i;
$j=$i;///then i want to try to return $j--but i get nothing
}
}

I guess I am a little confused.
Link to comment
Share on other sites

I thought that I had it...but I didn't.

$query = mysql_query("SELECT avg(Days) AS columnaverage FROM More_Metrics WHERE More_Metrics.Topic_Title='$title'");
$array = mysql_fetch_result($query);
$totalaverage = $array[columnaverage];
echo "$totalaverage";

I received the message:
Fatal error: Call to undefined function mysql_fetch_result() in /export/home2/webpages/more_metrics.php on line 422

The error before that didnt recognize the AVG() fucntion. What am I doing wrong? It seems correct to me.
Link to comment
Share on other sites

Okay--I switched to mysql_fetch_assoc but it only return the last number in the column. I wrote the code 2 ways and I received the same result. Any suggestions?

1:
$query = mysql_query("SELECT avg(More_Metrics.Days) AS columnaverage FROM More_Metrics WHERE More_Metrics.Topic_Title='$title'");
$array = mysql_fetch_assoc($query);
$totalaverage = $array[columnaverage];
echo "$totalaverage";


2:
$sql= "SELECT AVG(More_Metrics.Days) FROM More_Metrics WHERE More_Metrics.Topic_Title='$title'";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
$line = mysql_fetch_array($result, MYSQL_ASSOC);
foreach ($line as $col_value) {
    echo "\t\t<td>$col_value</td>\n";
}


Link to comment
Share on other sites

[quote author=gladiator83x link=topic=101825.msg403434#msg403434 date=1153862004]
no i'm really just returning the last cell in my column--which doesn't come close to what the avg should really be-- i'm just confused. is there any other way to do this?
[/quote]

The way you've been shown should work. Are you sure of the values in the table? Can you look at all the rows that have column TopicTitle with the value you are using and work out what value to expect? What happens if you leave the WHERE clause out of the query?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.