Php 我想要添加while循环浮点值
我在PHP中添加浮点值时遇到了一个问题 我试过很多次,但我不知道怎么做。 下面是我的代码Php 我想要添加while循环浮点值,php,mysql,mysqli,Php,Mysql,Mysqli,我在PHP中添加浮点值时遇到了一个问题 我试过很多次,但我不知道怎么做。 下面是我的代码 $selctAllAmount = "SELECT * FROM amount WHERE id = '$id'"; if($resultAmount = mysqli_query($conn, $selctAllAmount)){ if(mysqli_num_rows($resultAmount) > 0){ while($row = mysqli_fetch_array
$selctAllAmount = "SELECT * FROM amount WHERE id = '$id'";
if($resultAmount = mysqli_query($conn, $selctAllAmount)){
if(mysqli_num_rows($resultAmount) > 0){
while($row = mysqli_fetch_array($resultAmount)) {
$AmountAll = $row['amount'];
$counts = count($AmountAll);
echo $counts;
}
}
}
$AMONTALL变量具有两个浮点值,如0.5、0.6、0.9和0.3
如何计算所有这些值,例如0.5、0.6、0.9、0.3=2.3
请帮我找个人谢谢你必须这样做
$AmountAll += $row['amount'];
或
例如,如果要在SQL查询中求和,请使用SUM
SELECT SUM(amount) As total FROM tablename
循环遍历结果,并将其添加到每个迭代的变量
$total
此外,在查询中使用变量输入时,请确保使用准备好的语句
$selctAllAmount = "SELECT amount FROM amount WHERE id = ?";
if($stmt = $conn->prepare($selctAllAmount)) {
$stmt->bind_param("s", $id);
$stmt->execute();
$stmt->bind_result($amount);
$total = 0;
while ($stmt->fetch()) {
$total += $amount;
}
$stmt->close();
echo $total;
}
更好的是,使用SUM()
运行单个查询
应该使用
SUM(amount)作为总金额
而不是*
,然后只取那一行,然后不需要循环。这个问题中的任何内容都与标记的JavaScript或html无关。仅使用与问题直接相关的标记如果我有2.3,如何减去$AmountAll,然后减去$AmountAll-2$AmountAll-=$row['amount'];如果我从所有行中选择了4.4卢比进行捐赠,并且我正在捐赠1卢比,那么我如何用1或2个任意数字减去$AmountAll,并将减去的值发送到数据库浮动值$AmountAll=$AmountAll-1sir,那么我如何用3.4之间的钱更新所有行,比如0.6、0.7、0.4、0.9,0.4,0.4请帮助我先用循环划分3.4,然后用行更新
$selctAllAmount = "SELECT SUM(amount) as amount FROM amount WHERE id = ?";
if($stmt = $conn->prepare($selctAllAmount)) {
$stmt->bind_param("s", $id);
$stmt->execute();
$stmt->bind_result($amount);
$stmt->fetch();
echo $amount;
$stmt->close();
}