Jump to content

Simple question about adding values


patheticsam

Recommended Posts

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

$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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.