How can the use of LIKE in SQL queries lead to vulnerabilities such as SQL injection in PHP applications?
Using the LIKE operator in SQL queries without proper sanitization can lead to SQL injection vulnerabilities in PHP applications. This is because user input directly inserted into the query can be manipulated to execute malicious SQL commands. To prevent this, it is crucial to use prepared statements with parameterized queries to securely handle user input.
// Using prepared statements to prevent SQL injection
$searchTerm = $_POST['searchTerm'];
$stmt = $pdo->prepare("SELECT * FROM table WHERE column LIKE :searchTerm");
$stmt->execute(['searchTerm' => '%' . $searchTerm . '%']);
$results = $stmt->fetchAll();