patheticsam Posted March 28, 2010 Share Posted March 28, 2010 Hi! I just have a small question since I'm a little bit new to php. Let me explain : I have a SELECT command which will go get some data from a MySQL and display it on a page. Here's the code : $data2 = mysql_query("SELECT * FROM `inventory` WHERE item_id='$info[item_id]'") or die(mysql_error()); while($specs = mysql_fetch_array( $data2 )) { echo " One of the rows is called `price` and it's DECIMAL(into db) so I'm outputting the data : $specs['price'] Since there's more than one value output i'd like to have them additioned and get the total. How can I do that? Any help will be appreciated! Thanks Link to comment https://forums.phpfreaks.com/topic/196825-simple-question-about-adding-values/ Share on other sites More sharing options...
andrewgauger Posted March 28, 2010 Share Posted March 28, 2010 $total=0; $data2 = mysql_query("SELECT * FROM `inventory` WHERE item_id='$info[item_id]'") or die(mysql_error()); while($specs = mysql_fetch_array( $data2 )) { echo "price=".$specs[2]."<br>"; $total += $specs[2]; } echo "total =". $total; First I'd like to encourage you to use mysql_fetch_assoc because it will allow your code to allow changes to your database without requiring updating code. That said, I had to assume the price column was $data[2] in this example. Link to comment https://forums.phpfreaks.com/topic/196825-simple-question-about-adding-values/#findComment-1033268 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.