关于无效的agrument Foreach

时间:2016-08-29 16:52:27

标签: php

h3llo all,

我已创建此foreach以了解数据库中每个扇区有多少个框:

if (($date_start !='') AND ($date_end !='')) {
  foreach($qty_box_sector->query('
  SELECT *, COUNT(*)  
  FROM boxes
  LEFT JOIN sectors ON sector_id = box_sector_id
  LEFT JOIN users ON user_id = box_user_id
  WHERE box_user_id = '.$user_id.'
  AND box_create_date BETWEEN '.$date_start.' 00:00:00 AND '.$date_end.' 23:59:59
  GROUP BY box_sector_id') as $row) {
  ... echos here ...
}

一切正常,但当我尝试应用这样的AND / BETWEEN条件时:

AND box_create_date BETWEEN '.$date_Start.' 00:00:00 AND '.$date_end.' 23:59:59

我有这个警告

Warning: Invalid argument supplied for foreach()

什么错了? : - )

1 个答案:

答案 0 :(得分:2)

请尝试使用此选项,查询中的日期也必须用引号括起来

if (($date_start !='') AND ($date_end !='')) {
  foreach($qty_box_sector->query(
  "SELECT *, COUNT(*)  
   FROM boxes
     LEFT JOIN sectors ON sector_id = box_sector_id
     LEFT JOIN users ON user_id = box_user_id
   WHERE box_user_id = '$user_id'
     AND box_create_date BETWEEN '$date_start 00:00:00' 
                             AND '$date_end 23:59:59' 
   GROUP BY box_sector_id") as $row) {
  ... echos here ...
}