Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 正在计算数据库中的行总数_Php_Mysql - Fatal编程技术网

Php 正在计算数据库中的行总数

Php 正在计算数据库中的行总数,php,mysql,Php,Mysql,我试图计算我从数据库中得到的所有价格的总额,但我无法计算出来。这是餐馆的账单 $parameters = array(':tafelnummer'=>$tafelnummer); $sth = $pdo->prepare("select * from bestellingen WHERE Tafelnummer = '$tafelnummer'"); $sth->execute(); while($row = $sth->fetch()) {

我试图计算我从数据库中得到的所有价格的总额,但我无法计算出来。这是餐馆的账单

$parameters = array(':tafelnummer'=>$tafelnummer);

$sth = $pdo->prepare("select * from bestellingen WHERE Tafelnummer = '$tafelnummer'");
$sth->execute();




while($row = $sth->fetch())
{
    
    
    echo "<tr> ";
        echo "<td>" . $row['Name'] . "</td>"; 
        echo "<td>" . $row['Soort'] . "</td>";
        echo "<td> " . $row['Price'] . "</td>";
    echo "</tr> "; 
    

}

$parameters=array(':tafelnummer'=>$tafelnummer);
$sth=$pdo->prepare(“从bestellingen中选择*其中Tafelnummer='$Tafelnummer'”);
$sth->execute();
而($row=$sth->fetch())
{
回声“;
回显“$row['Name']”;
回显“$row['Soort']”;
回显“$row['Price']”;
回声“;
}
价格会给出我需要的每道菜的总金额

SELECT SUM(Price) AS Totalamount 
FROM bestellingen 
WHERE Tafelnummer = '$tafelnummer' 

使用此查询而不是获取总金额。

您可以保留一个累加器,然后将一个合计行添加到账单中,而不是返回数据库

我还试图修复您的SQL注入问题

$sth = $pdo->prepare("select * from bestellingen WHERE Tafelnummer = :tn");
$sth->execute([':tn' => $tafelnummer]);

$tot = 0;

while($row = $sth->fetch()) {
        
    echo "<tr> ";
        echo "<td>" . $row['Name'] . "</td>"; 
        echo "<td>" . $row['Soort'] . "</td>";
        echo "<td> " . $row['Price'] . "</td>";
    echo "</tr> "; 

    $tot += (float)$row['Price'];
}

echo "<tr> ";
    echo "<td>&nbsp;</td><td>TOTAL</td><td>$tot</td>";
echo "</tr> ";

$sth=$pdo->prepare(“从bestellingen中选择*,其中Tafelnummer=:tn”);
$sth->execute([':tn'=>$tafelnumer]);
$tot=0;
而($row=$sth->fetch()){
回声“;
回显“$row['Name']”;
回显“$row['Soort']”;
回显“$row['Price']”;
回声“;
$tot+=(浮动)$row[‘价格’];
}
回声“;
echo“总计$tot”;
回声“;

只需使用一个变量$total并将每个价格添加到while循环中即可

$total=0;

while($row = $sth->fetch())
{
    $total+=(int)$row["Price"];
    echo "<tr> ";
      echo "<td>" . $row['Name'] . "</td>"; 
      echo "<td>" . $row['Soort'] . "</td>";
      echo "<td> " . $row['Price'] . "</td>";
    echo "</tr> "; 
}
$total=0;
而($row=$sth->fetch())
{
$total+=(int)$row[“Price”];
回声“;
回显“$row['Name']”;
回显“$row['Soort']”;
回显“$row['Price']”;
回声“;
}

确保您的价格值始终为整数。没有必要将输入转换为(int)。

我还没有计算出价格的总和。我在网上到处寻找,试图找出答案,但还没有找到我的代码的解决方案。你的脚本是开放的。甚至您也应该始终在
MYSQLI_uquot>或
PDO
API中使用,而不是将用户提供的值连接到查询中。永远不要相信任何用户输入!谢谢你的帮助。我也可以在php文件中使用它并在屏幕上显示结果吗?$sth=$pdo->prepare(“从bestellingen中选择SUM(Price)作为Totalamount,其中Tafelnummer='$Tafelnummer'”)$执行某事物$行=$sth->fetch(PDO::fetch_ASSOC)$总和=$行['Totalamount'];是的,你可以使用上面的代码。我试过你的代码,它给了我这个结果。我制作了一个屏幕截图链接,这样你可以更好地查看结果:我做错了什么吗?字母现在不见了,但它仍然向我显示了结果2.5,它不是所有价格的总和(见图),改变了
$tot+=(float)$row['Price']将文本转换为浮动,其位置如下所示,但不计算所有价格。我忘了什么吗?-->对于你最近的一个问题,价格存储为小数。好吧,是什么改变了你突然接受了答案??自从意大利加入欧元以来,价格几乎从来都不是整数:)