Adding up all multiple rows

Discussion in 'PHP' started by adamjblakey, Nov 15, 2008.

  1. #1
    Hi,

    I have this but it does not work. I wish to add up all the rows but it does not add them. What am i doing wrong?

    
    $sql= mysql_query("SELECT * FROM `table` WHERE MONTH(date) = '$smonth'");
    	while ($row = mysql_fetch_assoc($sql)) {
    		$total_hours = $row['hours']++;
    	}
    $total = ($total_hours * 15);
    
    PHP:
    Cheers,
    Adam
     
    adamjblakey, Nov 15, 2008 IP
  2. rene7705

    rene7705 Peon

    Messages:
    233
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $total_hours = $total_hours + $row['hours'];

    and add this before the sql query:
    $total_hours = 0;
     
    rene7705, Nov 15, 2008 IP
  3. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #3
    $total_hours = $row['hours']++;
    Your code above keep re-assign a new value for $total_hours ( in fact, $total_hours + 1 )

    You should make it as:
    $total_hours += $row['hours'];

    Basically the same as the post above.
     
    ads2help, Nov 15, 2008 IP