Php select * where like

Php select * where like

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Combining Free-Text and Dropdown Filters in MySQL Queries

When building a search interface, it is common to mix a free-text input field with a specific filter, such as a dropdown menu for location. The challenge often arises not from fetching data individually, but from constructing a single query that respects both inputs simultaneously without returning empty or irrelevant results.

Consider a scenario where you have two inputs: one allowing users to type keywords and another letting them select a region like Antrim or Armagh. If your goal is to find projects that match the keywords regardless of location when no specific location is chosen, but only within the selected region when a dropdown value is provided, the logic requires careful handling of conditional clauses in your SQL statement.

A frequent mistake is attempting to use simple logical operators like OR across all conditions indiscriminately. In the example below, searching for keywords while also having a location selected might inadvertently return rows that match the keywords but are outside the selected region if not structured correctly. The query needs to ensure that the keyword search applies globally, while the location filter applies only when a value is explicitly present.

To achieve this in PHP using MySQLi, you must dynamically construct your WHERE clause based on whether the dropdown contains a value. If the user selects "Any Location" or leaves it blank (assuming the database stores 'Any Location' as null or an empty string depending on your schema), you should omit the location restriction entirely. However, if a specific region like 'Carlow' is selected, that condition must be appended to the query with an AND operator, not an OR.

Here is how you can structure the query logic:

$keywords = $_POST['keywords'];
$keylocation = $_POST['location'];

// Assume $con is your established connection from connect.php
$sql = "SELECT * FROM projectitem WHERE 1=1";

if (!empty($keywords)) {
    // Use parameterized queries or proper escaping to prevent SQL injection
    $sql .= " AND (description LIKE '%$keywords%' OR item LIKE '%$keywords%' OR location LIKE '%$keywords%')";
}

if ($keylocation && $keylocation !== 'Any Location') {
    // Only add the location filter if a specific region is selected
    $sql .= " AND location = '$keylocation'";
}

$result = mysqli_query($con, $sql);

This approach ensures that if a user types "bridge" and selects "Antrim", the system searches for "bridge" in description, item, or location columns, but strictly limits results to rows where the location column equals 'Antrim'. If the user leaves the location as "Any Location", the query ignores the location column entirely, effectively broadening the search scope.

It is also worth noting that using LIKE against text fields like description or item can be performance-intensive on large datasets because it prevents the database from using standard B-tree indexes efficiently. If your tables contain thousands of rows, consider indexing these columns or implementing full-text search capabilities if exact substring matching becomes a bottleneck. Additionally, always ensure that user inputs are escaped before concatenation into SQL strings to mitigate the risk of injection attacks, especially when dealing with text fields that might contain special characters like quotes or backslashes.

By dynamically building your query conditions, you maintain flexibility for users who want broad searches while respecting their specific regional filters, ensuring accurate results regardless of the combination of inputs provided.

Our network