gpt4 book ai didi

php - foreach 来自总和查询的关联键

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:49 25 4
gpt4 key购买 nike

我使用字段求和从表中获取记录从销售额中选择 sum(net)、sum(vat)、sum(so_total)如下图

 $s=mysqli_query($con,"SELECT sum(net),sum(vat),sum(total) from sales");
$result=mysqli_fetch_array($s);
foreach($result as $data)
{
$so_net = $data['net'];
$so_tax = $data['vat'];
$fx_rate = $data['total'];

}

Error undefined index net
undefined index vat
undefined index total

当我们在 mysql 中运行查询时,如何只获取 sum(net) sum(vat) sum(total) 的字段名称。

最佳答案

您必须为每个字段创建别名,使用它可能对您有帮助

$data=mysqli_query($con,"SELECT sum(net) as net ,sum(vat) as vat ,sum(total) as total from sales");
$result=mysqli_fetch_array($data);
foreach($so_result as $result){
$so_net = $data['net'];
$so_tax = $data['vat'];
$fx_rate = $data['total'];
}

关于php - foreach 来自总和查询的关联键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40420868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com