PHP Mysql help! (Solved!!) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

PHP Mysql help! (Solved!!)

I am try to add up all the numbers in a column in my database! but i don't under stand how to do it. can someone explain this to me in detail please? $sql = "SELECT SUM(loss) AS 'loss_total' FROM LossTable"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "<div id=info>

quot;.$row['loss_total']."</div>"; } } else { echo "0 results"; } i want to add together all the numbers in the loss column

30th Jun 2017, 11:55 PM
Metrik 🕵
Metrik 🕵 - avatar
5 Answers
+ 2
You can easily use a + operator in your select. For example, lets say you have 5 columns: SELECT col1 + col2 + col3 + col4 + col5 FROM LossTable Let me know if that's what you were seeking to do or not. I'll be more than happy to further explain if needed, or come up with another solution. Take care!
30th Jun 2017, 2:25 PM
AgentSmith
+ 1
Netkos Ent Thank you for you answer. is there a way to do that if I am only using just loss?
30th Jun 2017, 3:00 PM
Metrik 🕵
Metrik 🕵 - avatar
+ 1
Sorry, I was on lunch at work. So you're wanting to calculate the sum of the column rather than the row? If loss is the only column, then there isn't anything to calculate for the row. Let me know and I have two ways you can go about it.
30th Jun 2017, 4:38 PM
AgentSmith
+ 1
your good. i am at work also. ya sorry it would be just one column(loss). Sorry still try to learn databases. Thank you for all the help
30th Jun 2017, 6:53 PM
Metrik 🕵
Metrik 🕵 - avatar
+ 1
Netkos Ent, I have found a solution to my problem!YA Thank you for all your help. and i would still like to here your solutions for a better understanding when you get time.
30th Jun 2017, 11:56 PM
Metrik 🕵
Metrik 🕵 - avatar