SELECT SUM PHP MySQL问题

时间:2010-05-29 20:21:03

标签: php

这让我疯了!下面你会找到我的PHP / MySQL代码,但我会在这里发布直接的mySQL语句:

   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'

当我在phpMyAdmin中执行此语句时,我得到与“msa”相关联的ot.value的总和。虽然,当我运行我的PHP代码时,它不会返回值。有人看到问题吗?

// 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>";

3 个答案:

答案 0 :(得分:0)

对于这些情况,请在执行mysql_query命令之前打印出SQL语句:

print($monthly_sales_amount_sql)
$result = mysql_query($monthly_sales_amount_sql);

提醒:Your query is susceptible to SQL Injection attacks

答案 1 :(得分:0)

AND o.sales_rep_id = '" . $sales_rep_id "'";

$sales_rep_id之后有一个语法错误,因为你错过了一个连接运算符。

答案 2 :(得分:0)

您应该进行标准调试。

显示您尝试执行的sql字符串,以确保它确实是您认为的那样。这是执行这些操作时最常见的错误。

检查来自php,语法,连接等的mysql错误。