MySQL LIKE Statement and PHP Variable
I need my following code to work.
I am trying to use a PHP Variable and also add a [charlist] Wildcard statement to it
Could some one please help figure out why it wont work.
Basically it works if i remove the [charlist] Wildcard but I need it to find all the letters which are in the PHP Variable
my code is as followed
LIKE ''[' $searchW开发者_StackOverflow社区ord ']%''
To use a character class, you need to use the REGEXP operator.
Additionally, after a character class, you need to indicate a repetition operator. % matches any string (and is only for LIKE), but if you want to apply it so that it will match any series of letters contained within your character class, you need to do:
$query = '`column` REGEXP "[' . $searchWord . ']+"';
Alternatively, use a * to match 0 or more. (+ is 1 or more)
If $searchWord is an array, try it by calling implode first on it:
$listOfCharacters = str_split($searchWord, 1);
$implodedString = implode(',', $listOfCharacters;
The imploded string is a comma seperated string now instead of an array. PHP doesn't convert arrays to string by itself. Then you can probably use it like:
LIKE ''[' $implodedString ']%''
Although I'm suspicious about using it without string concatonation here. Do we miss some piece of code here?
精彩评论