Jump to content

Math on mysql


gokihar

Recommended Posts

Hi, I'm new here !

I have some problems and hope you can help me a bit :

Got table with :

Product | quantity | price | (quantity*price) AS value

It all works fine but I cannot get the sum of value from all of the products.

 

In first part of the code I got select and table draw. After closing table I try to sum value :

$query2="SELECT sum(quantity*price) as value2 FROM table";
if($query2){echo " Command q2 is successful ";}
else {echo " Command q2 is not successful ";} 
$result2=mysql_query($query2);
if($result2){echo " Command r2 is successful ";}
else {echo " Command r2 is not successful ";} 
$zmienna=mysql_fetch_array($query2);
if($zmienna){echo " Command z is successful ";}
else {echo " Command z is not successful ";} 

echo " Result: ".$query2['value2'];

 

query2 give me "S" as output and I just don't know why. Any hints ?

Link to comment
Share on other sites

I'm new to mysql as well...

 

I don't think you can have a sum function in a query like that.

 

Maybe something along the lines of:

 

$sum_1 = sum(quantity*price)

 

and remove "sum(quantity*price) as value2" from $query2.

Link to comment
Share on other sites

FYI, in your first code, which I don't really get, you would change the last bit to (comments in the code):

$zmienna=mysql_fetch_array($result2); //need to fetch the result2, not query2
if($zmienna){echo " Command z is successful ";}
else {echo " Command z is not successful ";} 

echo " Result: ".$zmienna['value2'];  //need to use zmienna, because it holds the result set (array data), not query2 which is just a string

Link to comment
Share on other sites

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.