sqli总和列代码不起作用

时间:2016-03-27 18:20:09

标签: mysqli sum

我试图通过以下鳕鱼对一列数据库表进行求和,但它不起作用:

$sqli = "SELECT sum(total_salary) AS totSalary FROM allsalary WHERE month_session = '$month_name' and org_session = '$org_name' and desig_session = '$desig_name' ORDER BY ID ASC ";
$stmt = $mysqli->prepare($sqli);
$stmt->execute();
$stmt->store_result();
$totl = $stmt->fetch();
$total_salary = $totl['totSalary'];
echo $total_salary;

但我可以正确地取桌子。

现在我要总结名为' total_salary'的列,我需要一个建议......

1 个答案:

答案 0 :(得分:0)

你在这里缺少两个基本功能,bind_param()和bind_result()

$sql = "SELECT sum(total_salary) FROM allsalary WHERE month_session = ? 
        and org_session = ? and desig_session = ? ORDER BY ID ASC";
$stmt = $mysqli->prepare($sql);
$sttmt->bind_paran("sss",$month_name,$org_name,$desig_name);
$stmt->execute();
$stmt->bind_result($total_salary);
$stmt->fetch();

echo $total_salary;