Calculating SUM of entries in MySQL using PDO

Hey Guys,

I am developing a PHP application for my boss so he can keep track of recyclable materials in the warehouse.

I have one table in my MySQL database called [daily_recyclables]. inside this table i have 12 columns.

recID (INT11 AUTO INCREMENT),date,subgrade,castaluminium,copper,stainlesssteel,plastic,batteries,brass,lowgradePCB,mediumgradePCB,highgradePCB

stackexchange.com blocked me for asking this question for some reason… I am trying to get individual weight calculations for each material item to display in a statistics card (bootstrap 5), for Example for the month on June 1500KG was processed (based on what the employee enters into the system daily of course).

To get the total weight of COPPER for example, i tried using the following code:

<?php
$hostname="localhost";
$username="USERNAME";
$password="PASSWORD";
$db = "ecemsapp";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT SUM(weight) 
FROM daily_recyclables WHERE column = "copper"') as $row) {
echo "<tr>";
echo "<td>" . $row['SUM(weight)'] . "</td>". " KG";
echo "</tr>"; 
}
?>

The above produces nothing of course. What am i doing wrong?

You can use COUNT() instead of SUM …