Jump to content
Sign in to follow this  
mal14

How to display SQL Sum query in html?

Recommended Posts

I've come up with an SQL sum query:

$query = "SELECT SUM(calories) FROM tracklog AS calories WHERE userid = $_SESSION[userid]";

No errors are shown, however how do I get this value to be displayed using HTML?

 

I want it to be displayed after 'Total number of calories lost to date:'

Edited by mal14

Share this post


Link to post
Share on other sites

is the coloum int ? :)

 

With html ? ..

I would use php and echo/print it :)

Edited by emptry

Share this post


Link to post
Share on other sites

is the coloum int ? :)

 

With html ? ..

 

I would use php and echo/print it :)

 

I tried doing this! 

 

while($row = mysqli_fetch_assoc($result))  
{ 


    echo 'Calories burned to date: '.$row['calories'];


    }

However, I keep getting any error saying calories is an undefined index

Share this post


Link to post
Share on other sites

Yes, because there is no “calories” column in your result set. The only column is “SUM(calories)”. To get a more handy name, use an alias:

SELECT SUM(calories) AS total_calories FROM ...;

Then the column is called “total_calories”.

Share this post


Link to post
Share on other sites

Yes, because there is no “calories” column in your result set. The only column is “SUM(calories)”. To get a more handy name, use an alias:

SELECT SUM(calories) AS total_calories FROM ...;

Then the column is called “total_calories”.

 

Thank you!!! It works :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.