Jump to content

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
https://forums.phpfreaks.com/topic/237508-math-on-mysql/
Share on other sites

Actually, looks like you can:

 

http://dev.mysql.com/doc/refman/5.0/en/select.html

 

The first comment there might help you:

 

SELECT

SUM(IF(beta_idx=1, beta_value,0)) as beta1_value,

SUM(IF(beta_idx=2, beta_value,0)) as beta2_value,

SUM(IF(beta_idx=3, beta_value,0)) as beta3_value

FROM alpha JOIN beta WHERE alpha_id = beta_alpha_id;

 

Link to comment
https://forums.phpfreaks.com/topic/237508-math-on-mysql/#findComment-1220499
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
https://forums.phpfreaks.com/topic/237508-math-on-mysql/#findComment-1220681
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.