您好我需要获取数据库字段sbstart的总和。我将代码更改为SUM(sbstart),但似乎不是有效的举动。我该如何显示总和?感谢。
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'regional_data';
@ $db = mysqli_connect($host, $user, $pass, $db);
if(mysqli_connect_errno())
{
die('The connection to the database could not be established.');
}
$query = "SELECT SUM(sbstart) FROM newchk WHERE dist_chk ='$distUsr'";
$result = mysqli_query($db, $query);
while ($recS = mysqli_fetch_array($result)){
echo ($recS['sbstart']);
}
?>
答案 0 :(得分:2)
更改
$query = "SELECT SUM(sbstart) FROM newchk WHERE dist_chk ='$distUsr'";
收件人(使用别名)
$query = "SELECT SUM(sbstart) as sub FROM newchk WHERE dist_chk ='$distUsr'";
然后
you doesn't need while loop because it return only one result.
$recS = mysqli_fetch_array($result);
echo $recS['sub'];
答案 1 :(得分:0)
您需要使用mysqli_fetch_assoc
代替mysql_fetch_array
,或者需要echo $recS[0]
。
答案 2 :(得分:0)
更改
$query = "SELECT SUM(sbstart) FROM newchk WHERE dist_chk ='$distUsr'";
到
$query = "SELECT SUM(sbstart) AS sum FROM newchk WHERE dist_chk ='$distUsr'";
并更改
echo ($recS['sbstart']);
到
echo ($recS['sum']);