Guber-X Posted August 27, 2012 Share Posted August 27, 2012 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 27, 2012 Share Posted August 27, 2012 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 27, 2012 Share Posted August 27, 2012 $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"; } Quote Link to comment Share on other sites More sharing options...
Guber-X Posted August 27, 2012 Author Share Posted August 27, 2012 thanks Psycho guess i just needed to change how i fetched the resaults Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.