Bring sql record even if you don’t have the necessary accent

Asked

Viewed 184 times

3

I need to bring a record called Vidraçaria in a survey, how can I bring this record by writing only Glassware in research?

Or bring water seeking only agua

The search works normally when the word is written correctly, with or without accent.

I’m using like() to fetch information in mysql records

  • I’ve seen this question and I couldn’t get any results

2 answers

4


Use a function to remove accents from the searched word and from what is in the bank. This way you will find the word with or without accents:

function tiraacentos($i){
   return preg_replace(array("/(á|à|ã|â|ä|Á|À|Ã|Â|Ä)/","/(é|è|ê|ë|É|È|Ê|Ë)/","/(í|ì|î|ï|Í|Ì|Î|Ï)/","/(ó|ò|õ|ô|ö|Ó|Ò|Õ|Ô|Ö)/","/(ú|ù|û|ü|Ú|Ù|Û|Ü)/","/(ñ|Ñ)/","/(ç|Ç)/","/(ý|ÿ|Ý)/"),explode(" ","a e i o u n c y"),$i);
}

In the query you use the function in the BD table name and the search term:

$palavra = $_POST['palavra'];
"SELECT * FROM tabela WHERE ".tiraacentos(NOME DA TABELA)." LIKE '%".tiraacentos($palavra)."%'"

1

You can use a collation different at runtime to remove accents:

...
WHERE nome like '%vidracaria%' collate utf8_general_ci

Or in case of pointed error:

...
WHERE nome like '%vidracaria%' collate utf8mb4_general_ci
  • Brings the following message from Heidisql "Collation 'utf8_general_ci' is not Valid for Character set 'utf8mb4'"

  • @R.Wegner Try the same command above but exchange utf8_general_ci for utf8mb4

  • Did not return any error, but also did not return anything

Browser other questions tagged

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