MySQL diacritic insensitive search (spanish accents)

前端 未结 5 753
孤街浪徒
孤街浪徒 2020-11-29 07:48

I have a MySQL database with words containing accents in Spanish (áéíóú). I\'d like to know if there\'s any way to do a diacritic insensitive search. For instance, if I sear

5条回答
  •  忘掉有多难
    2020-11-29 08:34

    Just in case someone else stumbles upon this issue, I have found a way that solves the problem, at least for me, without messing with character sets and collations inside MySQL queries.

    I am using PHP to insert and retrieve records from the database. Even though my Database, tables and columns are utf8, as well as the encoding of the PHP files, the truth is that the encoding used in the connection between PHP and MySQL is being made using latin1. I managed to find this using $mysqli->character_set_name(); where $mysqli is your object.

    For the searches to start working as expected, returning accent insensitive and case insentive records for characters with accents or not, I have to explicitly set the character set of the connection.

    To do this, you just have to do the following: $mysqli->set_charset('utf8'); where $mysqli is your mysqli object. If you have a database management class that wraps your database functionality, this is easy to apply to a complete app. If not, you have to set this explicitly everywhere you open a connection.

    I hope this helps someone out, as I was already freaking out about this!

提交回复
热议问题