Invalid sql query : select * from tblimgcontrols JOIN imgfirmcategories ON imgfirmcategories.ImgControlRef = ImgControlID JOIN tblfirmcategory ON FirmCategoryRef = FirmCategoryID where ImgControlIsVisible != 0 and FirmCategoryRef = 274∨dertype=asc and ImgBannerPlace = 'top_center' order by RAND() limit 1 === 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 'dertype=asc and ImgBannerPlace = 'top_center' order by RAND() limit 1' at line 8