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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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";
}

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.