Jump to content

mysql_query total SUM of colum inputs help!


Guber-X

Recommended Posts

what im trying to get is the "total" of one colum in my database. the colum is set as a integer and would like to add them together for a total.

 

[database name] = task

[colum name] = complete

 

this is what ive tried and i get no output for the "sumtask"

p.s. my $numrow works and it queried from another query lol.

<?php
$task_sum = mysql_query('SELECT SUM(complete) AS sumtask FROM task GROUP BY project_name')
			or die('Task_Sum query failed to load: '.mysql_error());
		echo 'Sum of Complete: '.$row['sumtask'].'<br />Row Count: '.$numrow.'<br />';
?>

Results:

Sum of Complete:

Row Count: 400

 

as you can see, i get nothing for the sumtask.

I hope im just a retard and cant see a simple error haha... but if anyone can help me out, please do :P

You assign the result of the query to $task_sum. You never try to fetch a row, and then you try to use $row without creating that variable. Since you are doing a group by, keep in mind that you could get more than one row as the result of this query, so if you just fetch one row and echo it, you don't know what project_name it is with.

Turn on error reporting and you should at least get a notice for the $row array.

$query = "SELECT project_name, SUM(complete) AS sumtask FROM task GROUP BY project_name";
$result = mysql_query($query) or die('Task_Sum query failed to load: '.mysql_error());

while($row = mysql_fetch_assoc($result))
{
    echo "Sum of {$row['project_name']}: {$row['sumtask']}<br />\n";
}

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.