|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-11-12 15:56 UTC] taco at procurios dot nl
Description: ------------ There is no good method to escape a string for use in a (mySQL) LIKE clause. In a query like "SELECT `foo` FROM `bar` WHERE `baz` LIKE '%" . $qux . "%'" the value of $qux should be escaped for both the query itself (like PDO::quote() does) as the LIKE clause (i.e. escaping % and _ characters. Using PDO the only way to escape a variable is using either PDOStatement::bindParam() or PDO::quote(). The first is not suitable for two reasons: 1. Not every query is a prepared statement 2. There is no way to escape % and _ (escaping them first will result in the \ being escaped: \% becomes \\%) The latter is not suitable because it will add quotes to the string, so you'll have to get rid of the quotes, escape % and _ and add the result to the query. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 21:00:01 2025 UTC |
You could do an concat in SQL, not sure whether there's a good way to do this from an API perspective. SELECT foo FROM bar WHERE baz LIKE CONCAT('%', 'vbebbt', '%')