views:

215

answers:

3

Hello,

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 search for "lapiz" (without accent), i'd like to get results containing the word "lápiz" from my db. The way I'm currently doing the query is as follows:

$result = mysql_query("SELECT * FROM $lookuptable WHERE disabled = '0' AND name LIKE '%$q%' OR productCode LIKE '%$q%' LIMIT $sugglimit");

This is for an online store, so I don't know what people will be searching for... "lapiz" is just and example.

alt text

Thanks!

A: 

Store a second version of the string that has been stripped of diacritics?

Amber
I don't know if I explained myself. Let's say $q is equal to "lapiz". I need to get back results from db that contain "lápiz". I think you are talking when $q is equal to "lápiz" and then you strip the diacritics, which is the opposite. Thanks!
Hectorcr7
@Hector What Amber meant is store a copy of the string without the accents in MySQL. Then you can always perform a search on that column. That is not needed though, answer forthcoming.
NullUserException
I heard there's a way to set mySQL to UTF8 and that way you can do diacritic insensitive comparison. Do you know anything about it?
Hectorcr7
+4  A: 

Character sets & collations, not my favorites, but they DO work:

mysql> SET NAMES latin1;
mysql> SELECT 'lápiz' LIKE 'lapiz';
+-----------------------+
| 'lápiz' LIKE 'lapiz' |
+-----------------------+
|                     0 | 
+-----------------------+
1 row in set (0.01 sec)

mysql> SET NAMES utf8;
mysql> SELECT 'lápiz' LIKE 'lapiz';
+-----------------------+
| 'lápiz' LIKE 'lapiz' |
+-----------------------+
|                     1 | 
+-----------------------+


mysql> SET NAMES latin1;
mysql> SELECT _utf8'lápiz' LIKE _utf8'lapiz' ;
+---------------------------------+
| _utf8'lápiz' LIKE _utf8'lapiz' |
+---------------------------------+
|                               1 | 
+---------------------------------+

A nice chapter to read in the manual:Character Set Support

Wrikken
Nice one. –––––
webbiedave
+1  A: 

If you set the table's charset to UTF-8 and the collation to utf8_*_ci (_ci means "case insensitive) MySQL will perform case and accent-insensitive searches by default

Read more about charsets and collations here:
http://dev.mysql.com/doc/refman/5.1/en/charset-charsets.html

I tested it and

"lapiz" matches: "lápiz," "lapíz," and "lapiz"
"nino" matches: "niño," "ninó," and "nino"

You can set up the collation of your table upon creation:

CREATE TABLE table ( ... )
CHARACTER SET uft8 COLLATE utf8_general_ci;

Or you can ALTER it if it already exists.For more info, read the manual (link above).
If you are using phpMyAdmin, you can select the collation when you create your table.

NullUserException
How do I set the table? I'm not very familiar with mySQL. Thanks!
Hectorcr7
@Hector refer to edit
NullUserException
I've modified my table (CubeCart_inventory) to uft_general8_ci and still doesn't work well. I modified my table with phpmyadmin (refer to image in original post). My webpage is www.carrodelectronica.com
Hectorcr7