好吧,PDO 的最弱点是无法调试它,因为准备好的语句实际上存储在服务器端。我花了一整天的时间来解决我根本无法理解的问题。所以,情况是这样的:
//the query itself
//Did other customers from the list buy the same style in last 3 months?
$end = date("Y-m-d H:i:s");
$start = strtotime("-3 month", strtotime($end));
$start = date('Y-m-d 00:00:00', $start);
$q = $db->prepare('SELECT COUNT(*) as totals FROM web_order
JOIN web_order_item ON web_order_item.od_id = web_order.od_id
WHERE web_order.p_vendor IN (:id)
AND od_pcode = :pcode
AND od_date BETWEEN :start AND :end
');
$q->bindValue(":pcode", '1008GD');
$q->bindValue(":id", $custList);
$q->bindValue(":start", $start);
$q->bindValue(":end", $end);
$q->execute();
echo "SELECT COUNT(*) as totals FROM web_order
JOIN web_order_item ON web_order_item.od_id = web_order.od_id
WHERE web_order.p_vendor IN ($custList)
AND od_pcode = '1008GD'
AND od_date BETWEEN '$start' AND '$end'";
$res = $q->fetchAll();
die(print_r($res));
有趣的地方来了——print_r($res)
打印空数组。
Array ( [0] => Array ( [totals] => 0 [0] => 0 ) )
虽然打印出具有完全相同查询的 echo 语句
SELECT COUNT(*) as totals FROM web_order JOIN web_order_item ON web_order_item.od_id = web_order.od_id WHERE web_order.p_vendor IN ('210','1107','2295','2452') AND od_pcode = '1008GD' AND od_date BETWEEN '2012-06-13 00:00:00' AND '2012-09-13 13:15:36'
如果我自己执行回显查询,我会得到“总计”列的值为 1。同时 PDO 语句不返回任何内容。
任何帮助将不胜感激。
更新: 虽然发布了答案,但我仍然不明白为什么这不起作用。$custList 实际上是一个字符串,而不是一个数组,因为我使用了
$custList = implode(",", $custList);