|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-02-04 05:35 UTC] vendor at visv dot net
Description:
------------
$q = "SELECT id, name FROM test WHERE name like '%:foo%';
$s = "carrot";
$dbh = new PDO('mysql:...', $user, $pass);
$sth = $dbh->prepare($q);
$sth->bindParam(':foo', $s);
$sth->execute()
while ($r = $sth->fetch()) {
print_r($r);
}
the above does not work. Adding PDO::PARAM_STR, and the
length argument do not help matters.
simply embedding $s in place of :foo does work. It also
works fine if I leave off the "'%" and "%'" parts
and $s == the column value. It just seems bindParam()
cannot cope with the '% %' parts in the query.
I do not find similar in your bugtracking system, nor
in user supplied notes (currently there are none).
Thanks.
Reproduce code:
---------------
See description.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 19 17:00:01 2025 UTC |
The bug reporter has erred in assuming that parameters can be replaced _inside_ delimited strings within the SQL statement; he or she is treating parameter markers like plain old PHP variables. Of course, that would lead directly to possible SQL injection, which is exactly what bound parameters are meant to avoid. (Also, the sample code provided is missing an ending double-quote on the first line.) I'm sure the application will work as intended if rewritten as follows: $q = "SELECT id, name FROM test WHERE name like :foo"; $s = "carrot"; $dbh = new PDO('mysql:...', $user, $pass); $sth = $dbh->prepare($q); /* prepend and append % around the user-supplied value to match anywhere in the NAME field */ $s = "%{$s}%"; $sth->bindParam(':foo', $s); $sth->execute() while ($r = $sth->fetch()) { print_r($r); }