Mysql LIKE cannot be found by full name

Asked

Viewed 221 times

1

I have a search system in Mysql + PHP, it does the search like this:

$nome = "Treinamento Da Venda à Entrega (Teste)";
$sqlPost = "SELECT id FROM tabela WHERE nome LIKE '%$nome%'";
$resultPost = mysql_query($sqlPost);
$buscou = mysql_num_rows($resultPost);
echo $buscou;

In the bank I have the following registered name:

Treinamento Da Venda à Entrega (Teste)

If I seek the word sale, he thinks, for handover he does, but I’m looking for exactly Sale-to-Delivery Training (Test) he doesn’t think, I’m using the wrong LIKE?

What I realized is that he only searches for one word, if I put more than one he doesn’t think.

  • Passes the complete code snippet

  • Hi, I put, I found it kind of irrelevant, because the doubt is more in the way LIKE works than in the code itself.

3 answers

0

In the parameter $sqlPost = "SELECT id FROM tabela WHERE nome LIKE '%$nome%'"; it will try to search the base for words that contain nome in the word so it does not return any value, because Treinamento Da Venda à Entrega (Teste)does not have nome in its context, and the "in that case it is not necessary. A recommendation is to use Like '% alguma coisa'; as an example Like 'a%'; it will look for values starting with the letter A or Like '%a'; then it looks for values ending with the letter A. More examples of how to use the Like, you can find in this link

In short, your Like to find the whole amount, in case Treinamento Da Venda à Entrega (Teste), you must pass the following parameter Like '"Treinamento Da Venda à Entrega (Teste)"'

  • No.... Mysql recognizes the variable this way. And if you look for it: Like '"Sale-to-Delivery Training (Test)"' it does not return

0

Search using the joker or by full name:

$sqlPost = "SELECT id FROM tabela WHERE nome LIKE '%$nome%' OR nome = '$nome'";

Or if you want a more comprehensive search by last names use:

SELECT id FROM tabela 
    WHERE nome LIKE REPLACE('$nome',' ','%') 
       OR nome LIKE '%$nome%'
  • 1

    It is an option, but if you search by half of the name, already of the error.

  • I did an update with a more comprehensive query

0


I ended up searching for REGEXP:

$nome = "Treinamento Da Venda à Entrega (Teste)";
$nome2 = str_replace(" ", "|", $nome);
$nomeBusca = "SELECT id FROM tabela WHERE nome REGEXP '".$nome2."'";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.