You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AND status = 1 ORDER BY `company`' at line 1 SQL= SELECT distinct u.*, u.state as 'state_id', s.name as 'state', s.short_name as 'state_abr', DATE_FORMAT(u.lastlogin,'%c/%e/%Y %r') as lastlogin, DATE_FORMAT(u.expire,'%c/%e/%Y') as fmt_expire, DATE_FORMAT(u.since,'%c/%e/%Y') as since, DATE_FORMAT(u.created,'%c/%e/%Y') as created, t.name as 'type_name', c.title as 'chapter_name', concat(u.first_name,' ',u.last_name) as 'membername', DATEDIFF(CURRENT_TIMESTAMP, `expire`) as 'account_days' FROM ao_members u LEFT JOIN `ao_states` s ON (u.state = s.id) LEFT JOIN `ao_members_type` t ON (t.id = u.type_id) LEFT JOIN `ao_communities` c ON (u.chapter_id = c.id) WHERE u.id > 0 AND chapter_id = AND status = 1 ORDER BY `company`