Find the first and last word of a record

1

I have a snippet of code that takes the first and last user name from one table and stores it in another table. For example.:

Users table: Carlos Drummond de Andrade

I take Carlos Andrade and store it in another table: Table selected_users

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

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

But it did not work!

    
asked by anonymous 03.07.2015 / 21:57

1 answer

2

If you want to get users who have the same first and last name, separate the string and search for each 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 with the first name Carlos and the last name Andade .

    
03.07.2015 / 22:06