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

Can PDO Prepared Statements Use Wildcards with LIKE Clauses?

Barbara Streisand
Release: 2024-11-28 08:40:11
Original
1033 people have browsed it

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();
Copy after login

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();
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template