Home >Database >Mysql Tutorial >Can PDO Prepared Statements Use Wildcards with LIKE Clauses?

Can PDO Prepared Statements Use Wildcards with LIKE Clauses?

Barbara Streisand
Barbara StreisandOriginal
2024-11-28 08:40:11978browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn