2017-05-09 56 views
0

我一直在寻找五天中的更好的一部分(没有笑话,不想打扰你们),但我似乎无法得到这个权利。我想使用PHP中的函数在屏幕上显示一列(技术)的总量。别担心,$ day部分可用于其他功能。我不认为这是问题的一部分。先谢谢你。如何使用MySQL和PHP在列中获得总和

function calc_tech($day){ 

    include("connect.php"); // include database connection 

    $res = $mysqli -> query(" SELECT * FROM sales WHERE day = $day ") or die($mysqli->error); // Fetch data from the table 

    while($val = $res -> fetch_array()){ 
     $tech_total = $val[tech] += $val[tech]; 
    } 

    echo $tech_total; // Echo the result 
    //echo "42"; 

    $res -> free(); // Free the query results 
    $mysqli -> close(); // Close mysqli object 

} 

我的数据库表看起来像这样:

enter image description here

+0

选择总和(技术); – JYoThI

+0

你想在这里输出什么? –

+0

那么,什么是不工作? –

回答

1

你需要做的SUM()查询列:

功能calc_tech($日){

include("connect.php"); // include database connection 

$res = $mysqli -> query(" SELECT SUM(tech) as sum FROM sales WHERE day = $day ") or die($mysqli->error); // Fetch data from the table 

$val = $res -> fetch_array(); 
    $tech_total = $val['sum']; 


echo $tech_total; // Echo the result 

}

+1

非常感谢!它现在完美运行! – adesgagne

0

1)'$day'应该用单引号括起来。

FOR SUM of column name查询应该是这样的,从哪天='$当天的销量

"select sum(tech) as Toatal from sales where day='$day';" 
+0

这确实有助于谢谢你! – adesgagne

+0

很高兴帮助你@adesgagne – JYoThI