0

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'
AND (
tipoCuenta =2
OR tipoCuenta =3
)
AND (
estado_factura =1
OR estado_factura =3
)
AND modificado =0

enter image description here

I tried to replace the variables $primerFechaAnio and $primerFechaMeswith the values of the date but still not working, what I'm missing?

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Professor Zoom
  • 339
  • 1
  • 4
  • 13
  • So the error is obviously in your PHP code, right? – Strawberry May 16 '16 at 17:09
  • @Strawberry yes, it was my mistake :) – Professor Zoom May 16 '16 at 17:26
  • 1
    The `mysql_*` functions in PHP are deprecated and shouldn't be used. Please read [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) for information on why and what to replace them with. – Matt Raines May 16 '16 at 20:25

1 Answers1

1

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'];
Eduardo Galván
  • 962
  • 7
  • 15