prepare($sqlGeo); if ($stmtGeo) { $stmtGeo->bind_param("sss", $filterCity, $filterCity, $filterCity); if ($stmtGeo->execute()) { $stmtGeo->bind_result($lat, $lng); if ($stmtGeo->fetch()) { $centerLat = (float)$lat; $centerLng = (float)$lng; } else { $infoMsg = "Could not find coordinates for '{$filterCity}' in location table. " . "Filtering will use the town/city name only."; } } else { $errorMsg = "Geo lookup error: " . $stmtGeo->error; } $stmtGeo->close(); } else { $errorMsg = "Geo lookup error: could not prepare statement."; } } /* * STEP 2: Fetch landlord posts, using radius (Haversine) when possible, * otherwise simple filtering by city (or all posts if no filter). */ if (!$db) { $errorMsg = 'Database connection error.'; } elseif ($errorMsg === '') { // 2A. We have city + radius + coords -> use Haversine if ($filterCity !== '' && $radiusNumeric !== null && $centerLat !== null && $centerLng !== null) { // Earth radius ~3959 miles $sql = " SELECT l.id, l.gender, l.first_name, l.last_name, l.comments, l.event2, l.city, p.latitude, p.longitude, (3959 * ACOS( COS(RADIANS(?)) * COS(RADIANS(p.latitude)) * COS(RADIANS(p.longitude) - RADIANS(?)) + SIN(RADIANS(?)) * SIN(RADIANS(p.latitude)) )) AS distance FROM gbrLANDLORD l JOIN pcode1 p ON l.city = p.city OR l.city = p.place OR l.city = p.town HAVING distance <= ? ORDER BY distance ASC, l.id DESC "; $stmt = $db->prepare($sql); if ($stmt === false) { $errorMsg = 'Database query error: could not prepare statement.'; } else { // lat, lng, lat, radius $stmt->bind_param("dddd", $centerLat, $centerLng, $centerLat, $radiusNumeric); if ($stmt->execute()) { if (method_exists($stmt, 'get_result')) { // Easiest path if mysqlnd is available $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { $posts[] = $row; } } else { // Fallback if get_result() is not available $stmt->bind_result( $id, $gender, $first_name, $last_name, $comments, $event2, $city, $plat, $plng, $distance ); while ($stmt->fetch()) { $posts[] = [ 'id' => $id, 'gender' => $gender, 'first_name' => $first_name, 'last_name' => $last_name, 'comments' => $comments, 'event2' => $event2, 'city' => $city, 'distance' => $distance ]; } } $stmt->close(); } else { $errorMsg = 'Database query error: ' . $stmt->error; $stmt->close(); } } // 2B. Fallback: no valid radius or no coords -> simple name-based or full listing } else { $sql = "SELECT id, gender, first_name, last_name, comments, event2, city FROM gbrLANDLORD"; $types = ''; $args = []; if (