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 kontr_id = 0 AND stoki_id > 0 AND kontr_id > 0 ) OR ( stoki_id = AND g' at line 2SQL: SELECT cena FROM stoki_ceni_kontr WHERE ( stoki_id = AND kontr_id = 0 AND stoki_id > 0 AND kontr_id > 0 ) OR ( stoki_id = AND gr_kont_id = 0 AND stoki_id > 0 AND gr_kont_id > 0 ) OR ( kontr_id = 0 AND gr_stoki_id = AND kontr_id > 0 AND gr_stoki_id > 0 ) OR ( gr_kont_id = 0 AND gr_stoki_id = AND gr_kont_id > 0 AND gr_stoki_id > 0 ) ORDER BY cena DESC LIMIT 1