Jump to content

Recommended Posts

I Am Trying To Add All Of The Values In A Single Column In A Database. I Have Created The Code Bellow ( The Code To Connect Tho The DB Is Above This Code In The Script ):

 

 

$query = mysql_query("SELECT SUM(price) FROM products") or die(mysql_error());

$result = mysql_result($query, 0);

echo "Total Product Value: ",'$price';

 

 

but For Some Reason It Doesn't Work. There Are No Errors It Just Displays: "Total Product Value: $price "

 

Will Who Ever Awnsers Try To Give Me The Full Code Instead Of Just Saying How It Is Done... ( I'm An Egg Head. )

Link to comment
https://forums.phpfreaks.com/topic/111335-solved-total-value-of-a-column/
Share on other sites

Will Who Ever Awnsers Try To Give Me The Full Code Instead Of Just Saying How It Is Done... ( I'm An Egg Head. )

 

Yeah...no.  That's not how it works around here.  We're here to help, not write your code for you. Assuming your query didn't somehow fail:

 

echo "Total Product Value: $result";

 

if you put a variable inside single quotes it interprets it literally like you wanted to physically echo out $price not the value of price.  You can either put it inside the double quotes (like above) or use no quotes at all and use a . instead of a , between your string and the var.

 

Also, you didn't assign anything to $price. The result is in $result.

 

 

Try doing:

 

$query = mysql_query("SELECT SUM(price) as total FROM products") or die (mysql_error());
$result = mysql_fetch_assoc($query);
echo "Total product value: " . $result['total'];

 

Bare in mind, I actually don't know if this will work. I'm not used to using the mysql functions straight, but it looks right.

Try doing:

 

$query = mysql_query("SELECT SUM(price) as total FROM products") or die (mysql_error());
$result = mysql_fetch_assoc($query);
echo "Total product value: " . $result['total'];

 

Bare in mind, I actually don't know if this will work. I'm not used to using the mysql functions straight, but it looks right.

 

Your method is another way of doing the same thing he's doing.  PHP provides a slew of functions for handling the result source.  Most of them provide a basic means of getting and displaying the information.  They just have slight nuances for more efficiency, based on your circumstance and needs.  

 

His problem is that he used the wrong variable and on top of that he didn't echo it right.

Ok Well Rather Than Writing Another Tread I Will Just Post It Here... How Would You Move A Row To Another Table Based On The Content Of The Row?

 

Ex: The Person Enters A Barcode and PHP Trys To Match It To The One In The Database. If The Barcodes Are == Then Move The Row To Another Table.

 

You Can Explain or Give Code or What Ever You Think Will Help Me The Best. As I Said I Know Very Little Even Though I've Read Alot...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.