true, 'duplicates' => []]; try { $data = readJsonBody(); $bizName = trim($data['name'] ?? ''); $addressLine1 = trim($data['addressLine1'] ?? ''); $city = trim($data['city'] ?? ''); $state = trim($data['state'] ?? ''); $zip = trim($data['zip'] ?? ''); // Clean up city - remove trailing punctuation $city = preg_replace('/[,.\s]+$/', '', $city); $qDuplicates = queryTimed(" SELECT DISTINCT b.ID AS BusinessID, b.Name, a.Line1, a.City, s.Abbreviation AS AddressState, a.ZIPCode FROM Businesses b LEFT JOIN Addresses a ON a.BusinessID = b.ID LEFT JOIN tt_States s ON s.ID = a.StateID WHERE LOWER(b.Name) = LOWER(?) OR ( LOWER(a.Line1) = LOWER(?) AND LOWER(a.City) = LOWER(?) AND a.Line1 != '' AND a.City != '' ) ORDER BY b.Name ", [$bizName, $addressLine1, $city]); foreach ($qDuplicates as $row) { $addressParts = []; if (!empty($row['Line1'])) $addressParts[] = $row['Line1']; if (!empty($row['City'])) $addressParts[] = $row['City']; if (!empty($row['AddressState'])) $addressParts[] = $row['AddressState']; if (!empty($row['ZIPCode'])) $addressParts[] = $row['ZIPCode']; $response['duplicates'][] = [ 'BusinessID' => (int)$row['BusinessID'], 'Name' => $row['Name'], 'Address' => implode(', ', $addressParts), ]; } } catch (Exception $e) { $response['OK'] = false; $response['error'] = $e->getMessage(); } jsonResponse($response);