You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND e.webevent = bt.webevent AND e.periodicity = '0' ' at line 18
SELECT e.webevent, e.webparentevent, e.title, e.intro, DATE_FORMAT(e.startdate, '%d/%m/%Y') AS startdate, TIME_FORMAT(e.starttime, '%Hh%i') AS starttime, DATE_FORMAT(e.enddate, '%d/%m/%Y') AS enddate, TIME_FORMAT(e.endtime, '%Hh%i') AS endtime, e.location,, DATE_FORMAT(e.published,'%d/%m/%Y') AS published, DATE_FORMAT(e.archived,'%d/%m/%Y') AS archived, bt.priority, bt.genre, bt.public, bt.theme, bt.image FROM webevents AS e, webbtevents AS bt WHERE e.webfolder = AND e.webevent = bt.webevent AND e.periodicity = '0' AND e.startdate <= DATE_FORMAT('20161031', '%Y-%m-%d') AND (e.enddate >= DATE_FORMAT('20161025', '%Y-%m-%d') OR ISNULL(e.enddate) ) ORDER BY bt.priority DESC, e.startdate, e.starttime