0

In my query I have:

$search .= "and title REGEXP '[[:<:]]$q[[:>:]]' ";

Which produces the following error in the Apache error log:

PHP Parse error:  syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)

I tried putting {} around it, but that didn't work either.

Thank you.

6
  • 1
    Escape the dollar sign with a backslash. PHP is trying to interpret it as a variable. Or better yet, use a prepared statement and you don't have to worry about these things. Commented Aug 3, 2016 at 22:35
  • Prepared statements are imperative here. This is asking for serious problems. Commented Aug 3, 2016 at 22:42
  • @tadman, what do you mean serious problems? Commented Aug 3, 2016 at 22:57
  • I solved it by using this code prior to the actual query: $regexpVariable = "[[:<:]]" . $q . "[[:>:]]"; Commented Aug 3, 2016 at 23:24
  • @NestMan Look at what a tool like this can do to your site if you make a mistake of this magnitude. It's not a good list of things. Commented Aug 3, 2016 at 23:52

1 Answer 1

2

Add braces: $search .= "and title REGEXP '[[:<:]]{$q}[[:>:]]'"; -- otherwise it thinks you are referencing the $q array.

PHP allows array lookups in strings, such as "... $q[...] ..." It also allows expresssions: "... {$a+$b} ..." if you add the braces. Putting those together solves this problem.

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

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.