Home >Database >Mysql Tutorial >Can PDO Prepared Statements Use Wildcards with LIKE Clauses?
Wildcard Usage in PDO Prepared Statements
This inquiry seeks clarification on the feasibility of utilizing wildcards, specifically % for the LIKE clause, within PDO prepared statements.
Initially, unsuccessful attempts were made using bindParam. However, success was achieved upon switching to bindValue as follows:
$stmt = $dbh->prepare("SELECT * FROM `gc_users` WHERE `name` LIKE :name"); $stmt->bindValue(':name', '%' . $name . '%'); $stmt->execute();
Furthermore, the bindParam method can also be employed in this scenario with minor modifications:
$name = "%$name%"; $query = $dbh->prepare("SELECT * FROM `gc_users` WHERE `name` like :name"); $query->bindParam(':name', $name); $query->execute();
The above is the detailed content of Can PDO Prepared Statements Use Wildcards with LIKE Clauses?. For more information, please follow other related articles on the PHP Chinese website!