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 'order by ISNULL(rank),rank asc,photo_id' at line 1: select photo_id,added_on,caption,rank,longdesc from ic_gallery_photos where gallery_id = order by ISNULL(rank),rank asc,photo_id