如何使用MySQL和PHP在列中获得总和
问题描述:
我一直在寻找五天中的更好的一部分(没有笑话,不想打扰你们),但我似乎无法得到这个权利。我想使用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
}
我的数据库表看起来像这样:
答
你需要做的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
选择总和(技术); – JYoThI
你想在这里输出什么? –
那么,什么是不工作? –