Hid Dencum Hid Dencum - 3 months ago 22
PHP Question

About invalid agrument Foreach

h3llo all,

I have created this foreach to know how many box per sector are in database:

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 ...
}


All working good, but when I try to apply AND/BETWEEN condition like this:

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


I have this warning

Warning: Invalid argument supplied for foreach()


Whats wrong? :-)

Answer

Try this instead, the dates in your query have to be wrapped in quotes as well

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 ...
}
Comments