I current have a search form for a website that makes a search request to a mysql database using a keyword search and select tags. Right now it calls for all the results that the search bar is like. It then uses if statements to filter out the rest of the results bases on their information (If the current row doesnt have the request[searched] data in it then continue; on this while loop so that it skips that row when building the result array).
I know this isnt to good of a method because it loads a bunch of results I dont want then filters it within the while loop. What I am wondering is how I could filter the results within my query request to the DB so I dont need to filter everything after and increasing loading time.
When I tried to just filter the results with mysql this is how I thought maybe to do it, but it hasn't worked yet.
$sql = "SELECT * FROM mls_listings_phrets WHERE Address1 LIKE '%$searched%' or Remarks LIKE '%$searched%'";
$limit = "LIMIT " . ($page - 1)*$perPage . ", $perPage";
$endOfQuery = " ORDER BY id DESC " . $limit;
// if to search all, for sale, or for rent
if($ownershipType === "null"){
}else if($ownershipType === "rent"){
$sql .= " AND Lease NOT LIKE '%null%'";
}else if($ownershipType === "sale"){
$sql .= " AND Lease LIKE '%null%'";
}
$sql .= $endOfQuery;
$query = mysqli_query($connect, $sql);