SELECT SUM PHP MySQL problem

Posted by user345426 on Stack Overflow See other posts from Stack Overflow or by user345426
Published on 2010-05-29T20:21:02Z Indexed on 2010/05/29 20:32 UTC
Read the original article Hit count: 211

Filed under:

This is driving me nuts! Below you will find my PHP/MySQL code but I will post the direct mySQL statement here:

   SELECT SUM( ot.value ) AS msa
     FROM orders o
LEFT JOIN orders_total ot ON ot.orders_id = o.orders_id
    WHERE ot.class = 'ot_total'
      AND UNIX_TIMESTAMP( o.date_purchased ) >=1262332800
      AND UNIX_TIMESTAMP( o.date_purchased ) <=1264924800
      AND o.sales_rep_id = '2'

When I execute this statement inside of phpMyAdmin I get the sum for ot.value which is associated to "msa". Although, when I run my php code it does not return a value. Anyone see the problem?

// works in phpMyAdmin but not displaying during PHP execution!
$monthly_sales_amount_sql = "SELECT SUM(ot.value) AS msa 
                               FROM orders o 
                          LEFT JOIN orders_total ot ON ot.orders_id = o.orders_id 
                              WHERE ot.class = 'ot_total' 
                                AND UNIX_TIMESTAMP(o.date_purchased) >= $start_timestamp 
                                AND UNIX_TIMESTAMP(o.date_purchased) <= $end_timestamp 
                                AND o.sales_rep_id = '" . $sales_rep_id "'";                                       

$result = mysql_query($monthly_sales_amount_sql);
$row = mysql_fetch_assoc($result); 

echo "MSA: " . $row['msa'] . "<BR><BR>";

© Stack Overflow or respective owner

Related posts about php