"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Can PDO Prepared Statements Use Wildcards with LIKE Clauses?

Can PDO Prepared Statements Use Wildcards with LIKE Clauses?

Published on 2025-01-19
Browse:134

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();
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3