简体   繁体   English

PHP SQL除以零警告,但不为零

[英]PHP SQL division by zero Warning but is not zero

I have a trouble. 我有麻烦了 I have a SQL table with processes which are finalized, closed, cancelled and working on, which also are associateds by an area (talent, outsourcing, digital, etc). 我有一个SQL表,该表具有已完成,关闭,取消和正在处理的流程,这些流程也按地区(人才,外包,数字化等)关联。 I want to do an AVG by the area and total by the states of the processes. 我想按区域进行AVG,按流程状态进行总计。 so i have: 所以我有:

$tiempoQueryFinalizados = mysqli_query($con,
 "SELECT IFNULL(SUM(IFNULL(timestampdiff(DAY,fecha_creacion,IFNULL(fecha_cerrado,NOW())),0)),0) AS finalizados, 
    (Select count(id_proceso) from proceso where estado ='FINALIZADO' and fecha_cerrado is not null) as c 
  FROM proceso 
  WHERE estado = 'FINALIZADO' and fecha_cerrado IS NOT NULL and area ='".$rowArea['id_area']."' ");

Then I use a simple division in PHP with 然后我在PHP中使用一个简单的除法

mysqli_fetch_assoc($tiempoQueryFinalizados)['finalizados'] / mysqli_fetch_assoc($tiempoQueryFinalizados)['c']

but I get the error Warning: Division by zero , so I check if was zero but it doesn't. 但我收到错误Warning: Division by zero ,所以我检查是否为零,但不是。 In this example mysqli_fetch_assoc($tiempoQueryFinalizados)['c'] = 2 but still counting as 0 在此示例中, mysqli_fetch_assoc($tiempoQueryFinalizados)['c'] = 2但仍计为0

any help, please? 有什么帮助吗?

EDIT: the area is returned by a fetch array of $areaQuery = mysqli_query($con,"SELECT * from area where id_area!=0"); 编辑:该区域由$areaQuery = mysqli_query($con,"SELECT * from area where id_area!=0");的区域)的获取数组返回$areaQuery = mysqli_query($con,"SELECT * from area where id_area!=0");

This is not a simple division 这不是简单的划分

mysqli_fetch_assoc($tiempoQueryFinalizados)['finalizados'] / 
mysqli_fetch_assoc($tiempoQueryFinalizados)['c']

Each call to mysqli_fetch_assoc() gets a new row from your resultset. 每次对mysqli_fetch_assoc()调用都会从结果mysqli_fetch_assoc()获得新的一行。 So as the query only returns one row, the second call will return FALSE which equates to zero. 因此,由于查询仅返回一行,因此第二次调用将返回FALSE,它等于零。

So this would be a better way of doing the retrieval and calculation 因此,这将是进行检索和计算的更好方法

$row = mysqli_fetch_assoc($tiempoQueryFinalizados);
$calc = $row['finalizados']] / $row['c'];

Ok I forgot when every call you do to mysqli_fetch* you are jumping to the next register , so only I needed to edit 好的,我忘记了每次mysqli_fetch* 调用都跳到了下一个寄存器 ,所以只需要编辑

$tiempoQueryFinalizados = mysqli_query(...

by 通过

$tiempoQueryFinalizados = mysqli_fetch_assoc(mysqli_query(...

now $tiempoQueryFinalizados['finalizados'] / $tiempoQueryFinalizados['c'] 现在$tiempoQueryFinalizados['finalizados'] / $tiempoQueryFinalizados['c']

works very nice :) thank you all! 作品非常好:)谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM