How can the use of LIKE and % in SQL queries affect the search functionality in PHP applications?

Using LIKE and % in SQL queries can affect the search functionality in PHP applications by allowing for more flexible and wildcard-based search patterns. This can lead to better search results as users can search for partial matches or patterns within the data. However, it is important to sanitize user input to prevent SQL injection attacks when using LIKE and % in queries.

// Sanitize user input before using it in the SQL query
$searchTerm = mysqli_real_escape_string($conn, $_POST['searchTerm']);

// Construct the SQL query with LIKE and % to search for the input
$sql = "SELECT * FROM products WHERE product_name LIKE '%$searchTerm%'";

// Execute the query and fetch results
$result = mysqli_query($conn, $sql);