TapeGun007 Posted February 17, 2019 Share Posted February 17, 2019 This might be more of a mySQL question or a PHP question, I'm not exactly sure. I have a field in my table (Deals) called "Price". All I want is the sum of that field. I tried using something like: $price += $row['Price']; This ended up as one long string, even though the database is set as a decimal number field. Then I tried doing it from mySQL like this (from what I read), but I don't think I have the full answer: foreach($stmt->query('SELECT SUM(Price) FROM Deals') as $row) { echo "<tr>"; echo "<td>Total</td>"; echo "<td> ->".$row['SUM(Price)']."</td>"; echo "</tr>"; } Anyway, if someone could just show me how to get that sum, I would greatly appreciate it because I am missing something here. Quote Link to comment https://forums.phpfreaks.com/topic/308345-adding/ Share on other sites More sharing options...
benanamen Posted February 17, 2019 Share Posted February 17, 2019 (edited) ` Edited February 17, 2019 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/308345-adding/#findComment-1564526 Share on other sites More sharing options...
TapeGun007 Posted February 17, 2019 Author Share Posted February 17, 2019 5 minutes ago, benanamen said: ` Uh... thanks benanamen. Heheh. I kept searching, reading, and finally found the solution: $stmt = $pdo->prepare("SELECT SUM(Price) AS sumprice FROM Deals"); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); echo $row['sumprice']; Quote Link to comment https://forums.phpfreaks.com/topic/308345-adding/#findComment-1564527 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.