How can multiple search terms be implemented in a PHP MySQL query using the WHERE clause?

When implementing multiple search terms in a PHP MySQL query using the WHERE clause, you can use the AND or OR logical operators to combine conditions. This allows you to search for records that match all specified criteria (AND) or any of the specified criteria (OR). You can dynamically construct the query based on the search terms provided by the user.

// Assume $searchTerms is an array of search terms provided by the user
$searchTerms = ['term1', 'term2', 'term3'];

// Construct the WHERE clause dynamically based on the search terms
$whereClause = '';
foreach ($searchTerms as $index => $term) {
    $whereClause .= "column_name LIKE '%$term%'";
    if ($index < count($searchTerms) - 1) {
        $whereClause .= " AND ";
    }
}

// Execute the query with the constructed WHERE clause
$query = "SELECT * FROM table_name WHERE $whereClause";
$result = mysqli_query($connection, $query);

// Process the query result
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        // Process each row
    }
} else {
    echo "No results found.";
}