Search first and last word of a record

Asked

Viewed 442 times

1

I have a snippet of the code that picks up and the first and last user name of a table and stores in another table. For example.:

Table users: Carlos Drummond de Andrade

Picked up Carlos Andrade and stored in another table.: Table selected users

The system has an internal search from which it searches the first table users, but how would I make this search accurate, having in the client’s view only the first and last name? I tried to use LIKE.:

$sql = mysqli_query($conexao,"SELECT * FROM tab_usuarios WHERE NomeUsuarios LIKE '%".$nomeBusca."%' ");

But it didn’t work!

  • You want to search or extract the first and last words?

1 answer

2


If you want to get users who have the same first and last name, separate the string sought and seek every part of the name.

Example:

$nome = "Carlos Andrade";
$nome = explode(" ",$nome);
$primeiroNome = $nome[0];
$ultimoNome = $nome[1];
$sql = mysqli_query($conexao,"SELECT * FROM tab_usuarios WHERE NomeUsuarios LIKE '{$primeiroNome}%'  AND  LIKE '%{$ultimoNome}'");

This way your search will return all users who have the first name Carlos and the last name Andade.

  • 1

    I think he doesn’t want to seek, but rather get the first and last name.

  • 'Cause I’m in doubt too, let’s wait for his return.

  • Hello, everybody. Something like that.. rs rs... I need to search in the first user table the name and surname that were stored in the user table. For example: In the table users I have the user Carlos Drummond de Andrade, but it was stored in the table users_selected only Carlos Andrade. Only that in the internal search, if the client type Drummond, does not appear Carlos Drummond de Andrade, since the main search is being in the table users, but only if type Carlos or Andrade.

Browser other questions tagged

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