The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.1.2-1ubuntu2.14 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/showthread.php 28 require_once





× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Column Sum
#1
Im trying to add up the values of a single column but failing.....

<?php

$query = $db->query("SELECT SUM (sendout) from sendouts");
$count = $db->count();
echo $count;
var_dump($count);
?>
  Reply
#2
Count gives you the number of rows returned in your query. Use $db->first(); to return the first row which is all you will have given you are only selecting the sum. Also, after this, you will need to have the column you want to output.

I would:
-Change your select to be SUM(sendout) AS Total (or whatever you want to call it)
-Make your $count = to $db->first()->Total;

Then you can echo it Smile
  Reply
#3
If you're still having a problem with this and getting your query right, you can do something like this.

Code:
$count = 0;
Code:
$query = $db->query("SELECT sendout FROM sendouts")->results();
Code:
foreach ($q as $sum){
Code:
$count = $count + $sum->sendout;
Code:
}
Code:
echo $count;

It's important to establish
Code:
$count = 0
outside of your foreach loop first.
  Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)