jj20051 Posted June 22, 2008 Share Posted June 22, 2008 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. ) Quote Link to comment https://forums.phpfreaks.com/topic/111335-solved-total-value-of-a-column/ Share on other sites More sharing options...
.josh Posted June 22, 2008 Share Posted June 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111335-solved-total-value-of-a-column/#findComment-571548 Share on other sites More sharing options...
Darklink Posted June 22, 2008 Share Posted June 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111335-solved-total-value-of-a-column/#findComment-571549 Share on other sites More sharing options...
jj20051 Posted June 22, 2008 Author Share Posted June 22, 2008 Sorry About Asking For That "Code" but On Every Other Forum I Have Tried They Give Me 8 Lines Of Text and A Bunch Of Rambling... Quote Link to comment https://forums.phpfreaks.com/topic/111335-solved-total-value-of-a-column/#findComment-571553 Share on other sites More sharing options...
.josh Posted June 22, 2008 Share Posted June 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111335-solved-total-value-of-a-column/#findComment-571554 Share on other sites More sharing options...
.josh Posted June 22, 2008 Share Posted June 22, 2008 Sorry About Asking For That "Code" but On Every Other Forum I Have Tried They Give Me 8 Lines Of Text and A Bunch Of Rambling... “Give a man a fish; you have fed him for today. Teach a man to fish; and you have fed him for a lifetime” Quote Link to comment https://forums.phpfreaks.com/topic/111335-solved-total-value-of-a-column/#findComment-571556 Share on other sites More sharing options...
Darklink Posted June 22, 2008 Share Posted June 22, 2008 Yeah I noticed that whilst writing it. I have my own framework for MySQL functions. So I hardly ever look at the functions themselves anymore. The only time I do is when I update it. Quote Link to comment https://forums.phpfreaks.com/topic/111335-solved-total-value-of-a-column/#findComment-571558 Share on other sites More sharing options...
jj20051 Posted June 22, 2008 Author Share Posted June 22, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/111335-solved-total-value-of-a-column/#findComment-571563 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.