Failed to prepare the statement! SELECT SQL_CALC_FOUND_ROWS LENGTH(v.supporters) - LENGTH(REPLACE(v.supporters, ',', '')) AS 'scount',v.state,v.supporters,v.lat,v.lng,(3959 * acos( cos( radians() ) * cos( radians(v.lat ) ) * cos( radians(v.lng ) - radians() ) + sin( radians() ) * sin( radians( v.lat ) ) ) ) AS distance,v.b_id,v.genre,v.maincity,v.avgrating,v.totalcount,v.bpic,v.title,v.street,v.city,v.zip,count(r.b_id) as count,IFNULL(AVG(r.rating),0) as avg FROM vendors as v left outer join reviews as r on v.b_id=r.b_id GROUP by v.title HAVING v.maincity=? AND distance < 100 ORDER BY scount DESC,title LIMIT ?,10