How can multiple search terms be included in a MySQL database query in PHP?
To include multiple search terms in a MySQL database query in PHP, you can use the "LIKE" operator along with the "OR" condition. This allows you to search for records that match any of the specified search terms. You can dynamically construct the query string based on the search terms provided by the user.
// Assume $searchTerms is an array of search terms
$searchTerms = ['term1', 'term2', 'term3'];
// Construct the query string
$query = "SELECT * FROM table_name WHERE ";
foreach($searchTerms as $key => $term) {
if($key > 0) {
$query .= " OR ";
}
$query .= "column_name LIKE '%$term%'";
}
// Execute the query
$result = mysqli_query($connection, $query);
// Process the results
while($row = mysqli_fetch_assoc($result)) {
// Do something with the retrieved data
}