mysql_query, "like" and special characters

When you have to do a "search box" in a php/mysql website, you usually end with a SQL command like

select fields from table where field like '%what_to_search%'

but sometimes, this easy command does not run right. And "sometimes" means "when the searched string has special characters". I always use UTF-8 for everything, UTF-spanish-ci for collations, etc... Beside this, sometimes if i try to search "españa" (with the special character 'ñ'), there's no results. Then, i go to my mysql-admin program, run the query, and i get the desired result. So, why the php "mysql_query" command fails? I don't know, i tried all the tips and tricks i found on Google and nothing new. So the facts are: In some mysql servers i can't search a word with special characters (in some other servers, with the same code, i do the search and i obtain the desired result). Something happens with the mysql or php server config? I don't know, but if you search the same word without the special characters, you obtain what you searched for. If i search "españa", nothing. If i search "espana", i obtain the results for "españa" and "espana". So blow away all the special characters:

function convertspecialchars($input) { 
  $specialChars = "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèé êëìíîïðñòóôõöøùúûüýÿñ";
  $nonSpecialChars = "AAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaacee eeiiiionoooooouuuuyyn";
  return strtr($input, $specialChars, $nonSpecialChars);
}

Leave a comment
I have read and accept the Privacy Policy

The comments sent by each user will always be visible in the corresponding post, and will not be used for any other purpose. The user has the right to access, rectify and suppress said comments, as reflected in our Privacy Policy