Home  >  Q&A  >  body text

How to put variables in the MySQL query statement? Guys

I want to use php to query the database, but I have to add a variable to the condition

"select lyrics, song title, pinyin from name where pinyin like '%$cp[a-z]%'"

In this way, an error is reported directly. If you don’t add [a-z], there is no problem, but you must add it to query. What should I do? Guys

x梦x梦975 days ago858

reply all(6)I'll reply

  • autoload

    autoload2021-11-25 10:26:15

    In my impression, [] seems to be a regular thing. It doesn’t seem to be mentioned in the fuzzy query. You can try it

    reply
    0
  • autoload

    autoload2021-11-25 10:24:37

    select lyrics, song title, pinyin from name where pinyin like '%$cp%' and pinyin REGEXP '[a-z]'

    reply
    0
  • x梦

    Brother, is there any way to make the variable followed by a letter a-z? This method can only find the data containing $cp and a-z.

    x梦 · 2021-11-26 03:59:39
    autoload

    Okay, just put the variable in the regular expression

    autoload · 2021-11-26 08:51:02
    autoload

    Then you don't need to perform fuzzy query "select lyrics, song title, pinyin from name where pinyin REGEXP `$cp[a-z]`"

    autoload · 2021-11-26 08:58:12
    x梦

    No, brother, he will think that $cp[a-z] is an array variable and treat [] as a subscript. Parse error: syntax error, unexpected '-', expecting ']' in /www/wwwroot/xx.yuefenxiang.cn/ cs/fkyy.php on line 387. In the code, $cp[a is directly the color of the variable.

    x梦 · 2021-11-26 18:15:56
  • Cancelreply