Professor Zoom Professor Zoom - 5 months ago 12x
PHP Question

Aggregate function SUM() is returning null PHP

I got this problem, my query is as follow:

$obtener_egresos_cobrados = mysql_query("SELECT SUM(subtotal) AS egresos_cobrados FROM rg_egresos WHERE id = '".$sesionidCont."' AND DATE_FORMAT(fecha, '%Y') = '".$primerFechaAnio."' AND DATE_FORMAT(fecha, '%m') < '".$primerFechaMes."' AND (tipoCuenta = 2 OR tipoCuenta = 3) AND (estado_factura = 1 OR estado_factura = 3) AND modificado = 0") or die (mysql_error());
$obtener_egresos_cobrados = mysql_fetch_assoc($obtener_egresos_cobrados);
$total_egresos_cobrados = $obtener_ingresos_cobrados['egresos_cobrados'];

And it suppose to return an amount of 10,000 but only returns null, and when I try to do this in phpMyAdmin directly this return the right amount, this is what phpMyAdmin generate after the query was executed:

SELECT SUM( subtotal ) AS egresos_cobrados
FROM rg_egresos
WHERE id =6
AND DATE_FORMAT( fecha, '%Y' ) = '2016'
AND DATE_FORMAT( fecha, '%m' ) < '02'
tipoCuenta =2
OR tipoCuenta =3
estado_factura =1
OR estado_factura =3
AND modificado =0

enter image description here

I tried to replace the variables
with the values of the date but still not working, what I'm missing?


In your code you store the row in $obtener_egresos_cobrados, but you try to access $obtener_ingresos_cobrados. The last line in that code should be:

$total_egresos_cobrados   = $obtener_egresos_cobrados['egresos_cobrados'];