我试图用php从数据库中count()
数据,但它没有显示总数据,而是显示了数据。
这就是我的数
$query = "SELECT
mitra.*,
user.username,
user.privilege,
user.name
FROM
mitra
INNER JOIN
user ON mitra.id_user = user.id "
$result = $connection->query($query);
if ($result->num_rows > 0) {
foreach ($result as $row) :
$id = "" . $row["id"] . "";
$total = "" . $row["total_puas"] . "";
$privilege = "" . $row["privilege"] . "";
if ($privilege == 2) :
$calculate = $total / count($id);
var_dump(count($id));
endif;
endforeach;
}
===================
= id = total =
= 1 = 45.84 =
= 2 = 75.45 =
= 3 = 34.54 =
===================
当我var_dumb时,它显示
int(1)int(1)int(1)
而不是int(3)
我想要的。实际上我想用
$calculate
中应该有浮点数的数据和$total
来计数$total
我想用count id
除的$total
中的量有什么解决方案可以计算
count $id
中的金额,并且可以用45.84 + 75.45 + 34.54 / 3
划分为3?请帮忙我实际上试图从该表示例中进行的操作就像
最佳答案
您可以尝试以下代码:
<?php
$i = 0;
$total =0.00;
if ($result->num_rows > 0) {
while ($row = $result->fetch_array()):
if ($row["privilege"] == 2) :
$total = $total + $row["total"];
$i++;
endif;
endwhile;
echo $total."<br>";
echo $i."<br>";
echo $calculate = $total / $i;
}
?>
output
=====================================
$total = 155.83;
$i = 3;
$calculate = $total/$i;
$ans = 51.943333333333;
=====================================
关于php - 如何在PHP中以正确的方式获取count(*),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56880654/