I have a SUM query that isnt returning the correct value. It returns 1, not the value
There are 2 entries in the table both have the amount
column value 200 so the SUM should return 400 .. not 1
My query is below
Thanks
$owtbdq = mysqli_query($con,"SELECT * FROM `crm`.`workshop-otherwork` WHERE `wsjid` = '$wsjid'");
$owtbdr = mysqli_fetch_assoc($owtbdq);
$owtbdc = mysqli_query($con,"SELECT SUM(amount) FROM `crm`.`workshop-otherwork` WHERE `wsjid` = '$wsjid'");
$owtbdcount = mysqli_fetch_assoc($owtbdc);
I have also tried it without the ` around amount and same result
I have just ran
SELECT SUM(amount) FROM `crm`.`workshop-otherwork` WHERE `wsjid` = '4'
in HeidiSQL and it correctly returned 400 so havent a clue
Print a var_dump()
, I guess you are getting a resource link. Your query is correct, not your PHP code.
EDIT: sorry, it returns an identifier -> http://php.net/mysqli_fetch_assoc
$owtbdc = mysqli_query($con,"SELECT SUM(amount) FROM `crm`.`workshop-otherwork` WHERE `wsjid` = '$wsjid'");
if ($owtbdcount = mysqli_fetch_array($owtbdc)) {
echo $owtbdcount[0];
}
You should fetch the second request ($owtbdc)
$owtbdr = mysqli_fetch_assoc($owtbdc);
You have written $owtbdq
in mysqli_fetch_assoc($owtbdq);
where you are not doing SUM. you should do like this
mysqli_fetch_assoc($owtbdc);
$owtbdq = mysqli_query($con,"SELECT * FROM `crm`.`workshop-otherwork` WHERE `wsjid` = '$wsjid'");
$owtbdc = mysqli_query($con,"SELECT SUM(`amount`) FROM `crm`.`workshop-otherwork` WHERE `wsjid` = '$wsjid'");
$owtbdr = mysqli_fetch_assoc($owtbdq);
$row = mysqli_fetch_row($owtbdc)
$value = $row[0]
I guess this would work.
Fixed it
it was finally being returned as an array so did $newtotal = array_sum($owtbdcount)
that worked
thanks for help guys