我是PHP新手,想知道你是否可以帮助我选择总和查询?
我想根据user_id在我的数据库中添加费用表中的总金额?
这是我到目前为止所拥有的
<?php
//sets up thisPage
$pageSize=10;
if (isset($_POST["thisPage"]))
$thisPage = $_POST["thisPage"];
else
$thisPage=1;
//selects all distinct expenses that have been uploaded
$dbQuery="SELECT SUM(amount) AS TotalExpenditure FROM expenses WHERE user_id = '$userID'; ";
echo $dbQuery;
$dbResult=mysqli_query($db_connection, $dbQuery) or die (mysqli_error($db_connection));
echo "<table> <thead>";
//echo '<tr> <th>Project ID</th><th>Project Name</th></tr> </thead>';
while ($dbRow=mysqli_fetch_array($dbResult)){
// display row with expense
echo '<tr> <td>'. $dbRow['amount'] .'</td>';
}
echo "</table>";
echo "</form>";
?>
答案 0 :(得分:1)
你有
$dbRow['amount']
但你使用
SUM(amount) AS TotalExpenditure
// ^ this is the name (alias) of your column now
TotalExpenditure
是金额的名称,因此最后一部分应为
echo '<tr> <td>'. $dbRow['TotalExpenditure'] .'</td> </tr>';
答案 1 :(得分:0)
您必须使用TotalExpenditure
作为数组的键,所以
$dbRow['TotalExpenditure']
代替$dbRow['TotalExpenditure']
?
你给count()
sql函数添加了一个别名,所以你必须使用它