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 TypeID = 4 ORDER BY Priority DESC LIMIT 0,1' at line 1 (#1064) => SELECT ProductID FROM prd_Products WHERE Priority < AND TypeID = 4 ORDER BY Priority DESC LIMIT 0,1;