douguachi0056
2016-05-16 17:02
浏览 107
已采纳

聚合函数SUM()返回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'
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?

图片转代码服务由CSDN问答提供 功能建议

我遇到了这个问题,我的查询如下:

  $ obtener_egresos_cobrados = mysql_query(“SELECT SUM(subtotal)AS egresos_cobrados FROM rg_egresos WHERE id ='”。$ sesionidCont。“'和DATE_FORMAT(fecha,'%Y')='”。$ primerFechaAnio。“'和DATE_FORMAT(fecha)  ,'%m')&lt;'“。$ primerFechaMes。”'AND(tipoCuenta = 2 OR tipoCuenta = 3)AND(estado_factura = 1 OR estado_factura = 3)AND modificado = 0“)或die(mysql_error());  
 $ obtener_egresos_cobrados = mysql_fetch_assoc($ obtener_egresos_cobrados); 
 $ total_egresos_cobrados = $ obtener_ingresos_cobrados ['egresos_cobrados']; 
   
 
 

并且它假设返回10,000 但只返回null,当我尝试直接在phpMyAdmin中执行此操作时返回正确的数量,这是执行查询后phpMyAdmin生成的内容:

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

我尝试替换变量 $ primerFechaAnio $ primerFechaMes < / code>与日期的值但仍然不起作用,我缺少什么?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsjk3214 2016-05-16 17:07
    已采纳

    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'];
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题