PHP:如何在变量的列中获取值的总和

时间:2017-03-28 11:35:44

标签: php mysql

我正试图从表中获得薪水总和。我只需要回报总薪水。我试过了,

SELECT SUM(SalaryCol) FROM my_table;

如何在php中将查询结果作为变量?

3 个答案:

答案 0 :(得分:2)

SELECT SUM(SalaryCol) as total_salary FROM my_table;

答案 1 :(得分:0)

$sql = 'SELECT SUM(SalaryCol) as total_salary FROM my_table';

//$con is your connection variable
$result=mysqli_query($con,$sql);

while($row=mysqli_fetch_array($result,MYSQLI_NUM)){
   $salaries[] = $row['total_salary'];
}
return $salaries;

然后您可以将其作为数组访问并执行任何操作

答案 2 :(得分:0)

好的,我自己想通了。

$query = "SELECT SUM(SalaryCol) FROM my_table";

$sum = mysqli_fetch_row(mysqli_query($con, $query));

echo "Total : ".$sum[0];