Jump to content

Reading a table and adding up the results


Madatan

Recommended Posts

Okey so here is what I want to do.

I wanna get all the rows with ownerid = 1 and when I get them I wanna add the result in the column bonus for each row into one single variable.
So if I got let's say three rows with the ownerid 1, and every row got the column bonus to 4. Then I want a variable to be 4 + 4 + 4.

Anyone understanding or am I simply writing jibberish? :p

[code]
$query = "SELECT column FROM get_rows WHERE ownerid = '1'";
$result = mysql_query($query) or die('Error : ' . mysql_error());

$bonus = //I want the column results here.
[/code]
Same as always.

[code]
<?php
$query = "SELECT SUM(bonus) as total_bonus FROM tbl WHERE ownerid = 1;";
if ($result = mysql_query($query)) {
  if (mysql_num_rows($result) > 0) {
    $row = mysql_fetch_assoc($result);
    echo $row['total_bonus'];
  }
} else {
  echo mysql_error();
}
?>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.