What are the potential pitfalls of using LIKE in MySQL queries in PHP?

Using LIKE in MySQL queries can be inefficient, especially when searching for patterns at the beginning of a string. This is because MySQL cannot utilize indexes efficiently with leading wildcards. To solve this issue, consider using full-text search indexes or optimizing your queries to avoid leading wildcards.

// Example of optimizing a query to avoid leading wildcards with LIKE
$searchTerm = 'example';
$searchTerm = '%' . $searchTerm . '%';

$sql = "SELECT * FROM table_name WHERE column_name LIKE :searchTerm";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':searchTerm', $searchTerm, PDO::PARAM_STR);
$stmt->execute();
$results = $stmt->fetchAll();