mysql SUM()和php表中的动态行,供特定用户无法使用

I am having difficulties creating a dynamic table using a mysql database... my database has this;

userid | product | amount
2 | cat | 5
2 | dog | 3
2 | cat | 5
3 | cat | 1
3 | dog | 2

And I want to get a table that sums the amount of each product per user so that when the user visits a specific page, they can see their total of each product as (for example user 2);

Product | Amount
cat | 10
dog | 3

Here is my php code I had put together but it seems like my sum function isn't working and I cannot figure it out... Thanks in advance!

$userid = get_user_id()
$amountperproduct = mysqli_query($con,"SELECT $userid SUM(amount) FROM wp_payout_history GROUP BY product");

echo "<table border='1'>
<tr>
<th>product</th>
<th>grand sum</th>
</tr>";
if (mysqli_num_rows($amountperproduct)>0){
while($row2 = mysqli_fetch_array($amountperproduct))
{
echo "<tr>";
echo "<td>" . $row2['product'] . "</td>";
echo "<td>" . $row2['amount'] . "</td>";
echo "</tr>";
}
}
echo "</table>";

When using aggregation functions, you HAVE TO GROUP BY every other column that appears in the SELECT part (in your case: userid).

Now there seem to be many typos, such as the $ sign in the SELECT, missing comma etc.

You probably want a query as such:

SELECT userid, product, SUM(amount) AS total FROM wp_payout_history GROUP BY userid, product

This will create one large table, containing all the information you are looking for. If you want such a table for just one user, use a WHERE part, e.g.:

SELECT product, SUM(amount) AS total FROM wp_payout_history WHERE userid = '$userid' GROUP BY product