0

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 ''[' $searchWord ']%'' 
1
  • Nice SQL injection, I'll take it Commented Nov 18, 2009 at 10:32

2 Answers 2

3

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Say if I wanted it to work in the following way how could I do it: If $searchWord = AM how can I get it so display results which only have AM in the sentence ?
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.