Creating PDO Parameterized Query with LIKE Statement
Your original attempt at creating a PDO parameterized query with a LIKE statement had a small error. Here's the corrected code:
$query = $database->prepare('SELECT * FROM table WHERE column LIKE ?'); $query->execute(array('value%')); while ($results = $query->fetch()) { echo $results['column']; }
The only difference is in the execute() method. Instead of using a placeholder with a wildcard ("?%"), you should use a placeholder with a literal wildcard (% at the end). This tells PDO to add the literal wildcard to the value, which is what you want for a LIKE statement.
The above is the detailed content of How to Correctly Use PDO Parameterized Queries with LIKE Statements?. For more information, please follow other related articles on the PHP Chinese website!