I am new to PHP, and was wondering if you could help with my select sum query?
I am wanting to add the total amount from my expenses table in my database based on user_id?
Here is what I have so far
<?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>";
?>
You have
$dbRow['amount']
but you use
SUM(amount) AS TotalExpenditure
// ^ this is the name (alias) of your column now
TotalExpenditure
is the name of the amount, so the last part should be
echo '<tr> <td>'. $dbRow['TotalExpenditure'] .'</td> </tr>';
You have to use TotalExpenditure
as key of your array, so
$dbRow['TotalExpenditure']
instead of $dbRow['TotalExpenditure']
?
You're giving an alias to your count()
sql function so you have to use it