How to bypass an accent in mysql + Php

Asked

Viewed 1,773 times

3

I have in my database words with accents and when I want to call I use this SQL below.

But if a word in the database has accent I need to also use the word accented in my search, otherwise nothing returns.

How do I get around that situation and research both accented and accented words?

 SELECT * FROM produtos WHERE status_pr='online' AND (LCASE(nome_pr) LIKE '%$get%' 
 OR LCASE(descricao_pr) LIKE '%$get%' OR LCASE(marca_pr) LIKE '%$get%'
 OR LCASE(linha_pr) LIKE '%$get%' OR LCASE(categoria_pr) LIKE '%$get%')

In the code above, $get is the PHP variable that will be searched for.

  • 2

    Which charset and collate is being used? utf8, except for the leotard utf8_bin, ignores accentuation in the LIKE.

  • cherset is utf8_bin the collate I’m not using

  • utf8_bin is the collate.

3 answers

4

Copying Based on that response from the OS, you just have to select the fields using utf8 as charset:

SELECT * FROM produtos WHERE status_pr='online' AND (LCASE(nome_pr) LIKE _utf8'%$get%' 
 OR LCASE(descricao_pr) LIKE _utf8'%$get%' OR LCASE(marca_pr) LIKE _utf8'%$get%'
 OR LCASE(linha_pr) LIKE _utf8'%$get%' OR LCASE(categoria_pr) LIKE _utf8'%$get%')
  • Excellent tip, helped me a lot, thanks @Thomas.

3

  • 1

    Just remembering that the COLLATE utf8_unicode_ci already makes the comparison case insensitive, doing the function LCASE be unnecessary to the case.

0

Browser other questions tagged

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