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 '' at line 1 SQL query : SELECT DISTINCT productID, name1 as name, short_description1 as short_description, enabled, spec1, spec2, spec3, spec4, spec5, sort_order, create_date, update_date, visits, shops, ownerID, admin_name, code, imp_num, city, product_type as product_type_index, picture_count, deleted from adonis_products p LEFT JOIN (SELECT productID as prodID, categoryID FROM adonis_categories_products) cp ON p.productID=cp.prodID WHERE cp.categoryID in (2) AND p.enabled=1 ORDER BY RAND() LIMIT