Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

select sum of product

For some reason this isnt working and its most likley because im rough around the edges, any help please?

Trying to get the sum of a specific product and how many times its been ordered. I get incorrect values fromt the echo.

<?php $presum = $db->Execute("select sum(products_quantity) as products_ordered, products_id
                                                                        from ".TABLE_ORDERS_PRODUCTS . " 
                                                                        where products_id = '" . $products->fields['products_id']. "'
                                                                        group by products_id ");
                                                                        echo $presum->fields['products_ordered'];

thanks much
1 Solution
Muhammad WasifCommented:
Your sql is correct.

>>>I get incorrect values fromt the echo.
can you explain this?
if $products->fields['products_id'] = a single id, say 15, then you query will show sum of products for that specific ID

products_ordered= 200
products_id = 15

this will get u sum of products but not how many times it has been ordered. is that what u mean by not working ??

maybe u can use
Execute("select sum(products_quantity) as products_ordered, count(products_id) as q_ordered
                                                             from ".TABLE_ORDERS_PRODUCTS . " 
                                                            where products_id = '" . $products->fields['products_id']. "'
                                                            group by products_id ");

it should show
products_ordered= 200
q_ordered =10 (times)

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now