SQL Query + special characters ä, ö, ü, ø etc

血红的双手。 提交于 2019-12-12 06:01:54

问题


I'm wrapping my head around a problem for the last couple of days and need some tips on how to solve the problem.

I have a sql query which looks like this

    mysql_real_escape_string($value);
    $sql_first = "SELECT `user_id` FROM `usermeta` WHERE `meta_value` = '".$value."'


It works fine if $value isn't containing characters like ä ü ö ø

The collation of the database is utf8_general_ci

I tried various combinations with the php conv() function, but can't get it to work.

I guess I have to convert $value to UTF-8 - is this going in the right direction?


回答1:


Use prepared statements, and the database driver will do everything for you, and you won't be subject to SQL injection attacks as a bonus.

See http://php.net/manual/fr/mysqli.prepare.php




回答2:


I would say to try the utf8_unicode_ci collation

From this link:

Main differences are:

  1. utf8_unicode_ci supports so called expansions and ligatures, for example: German letter ß (U+00DF LETTER SHARP S) is sorted near "ss" Letter Œ (U+0152 LATIN CAPITAL LIGATURE OE) is sorted near "OE".

utf8_general_ci does not support expansions/ligatures, it sorts all these letters as single characters, and sometimes in a wrong order.

  1. utf8_unicode_ci is generally more accurate for all scripts. For example, on Cyrillic block: utf8_unicode_ci is fine for all these languages: Russian, Bulgarian, Belarusian, Macedonian, Serbian, and Ukrainian. While utf8_general_ci is fine only for Russian and Bulgarian subset of Cyrillic. Extra letters used in Belarusian, Macedonian, Serbian, and Ukrainian are sorted not well.

The disadvantage of utf8_unicode_ci is that it is a little bit slower than utf8_general_ci.

So when you need better sorting order - use utf8_unicode_ci, and when you utterly interested in performance - use utf8_general_ci.



来源:https://stackoverflow.com/questions/10563439/sql-query-special-characters-%c3%a4-%c3%b6-%c3%bc-%c3%b8-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!