Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialkarthikkn
5,397 Pointshow to do sum for sql query using php
<?php require_once 'includes/session.php'; ?> <?php require_once 'includes/token.php'; ?> <?php require_once 'includes/db.php'; ?> <?php require_once 'includes/functions.php'; ?> <?php require_once 'includes/header.php'; ?> <?php if (isset($_SESSION['name'])) { $message = "Welcome, " . $_SESSION['name']; $email = $_SESSION['name']; } else { redirect_to("index.php"); } $query = "SELECT * FROM users WHERE SUM(column_name) + 1"; $result = mysqli_query($connection, $query); if (!$result) { die("sorry"); } ?>
5 Answers
Shawn Gregory
Courses Plus Student 40,672 PointsKarthikkn
The SUM function in MySQL allows you to retrieve the sum of what's in a column of all the rows that match your query. Say you want to get the sum of all products in a database where the type of product is produce. Your table has 3 products under the type produce (3.50, 4.00, and 1.50). The following query will give you the sum of that column which will equate to 9.00:
SELECT sum(price) FROM products WHERE type = 'produce'
You would obtain the result by the following (assume you put the results returned in an array called $row):
$total = $row[sum(price)];
Hope this helps
Cheers!
karthikkn
5,397 Pointsthanks for help, Shawn But my question is i have add points to the column, After student signup as a minimum points to getting signup
Shawn Gregory
Courses Plus Student 40,672 PointsKarthikkn,
If I understand your question (and I'm sorry that I misinterpreted it before), you want to add 1 to the points that are already in the table to a row? If that is right then you would either want to set a default value to the points table when you create a user or just take out the value in the column, add 1, then update the column.
If you want to take the sum of all points for all users and add 1 to it, you would obtain the sum of all points in the table then add 1 to the value like so:
$points = $row[sum(points)] + 1;
Then you can do what you like with the new value using PHP or just return it to the table use UPDATE.
I hope this new information was what you are looking for.
Cheers!
karthikkn
5,397 PointsThanks again for your help, My question is when users perform some action he will get the points for that specific users, Who is logged in. example for my question treehouse quiz, When student answer the correct answer 12 points will be added to his account. Exactly same process for my project. Thanks again for your help
karthikkn
5,397 PointsThanks again for your help, My question is when users perform some action he will get the points for that specific users, Who is logged in. example for my question treehouse quiz, When student answer the correct answer 12 points will be added to his account. Exactly same process for my project. Thanks again for your help