1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

How to use PHP string in mySQL LIKE query? [duplicate]

Discussão em 'Outras Linguagens' iniciado por Stack, Abril 20, 2021.

  1. Stack

    Stack Membro Participativo

    I am trying to find the number of rows that match a specific pattern. In this example, all that START with "123":

    This is working:

    $query = mysql_query("SELECT * FROM table WHERE the_number LIKE '123%'");
    $count = mysql_num_rows($query);


    The problem is the LIKE will vary, so I'm trying to define it in the script, then execute the query, but this is NOT working:

    $prefix = "123";
    $query = mysql_query("SELECT * FROM table WHERE the_number LIKE $prefix.'%'");
    $count = mysql_num_rows($query);


    How can I get this query to work properly in the second example?

    EDIT: I've also tried it without the period (also not working):

    $query = mysql_query("SELECT * FROM table WHERE the_number LIKE $prefix'%'");

    Continue reading...

Compartilhe esta Página