php mysql jquery AJAX autocomplete case sensitivity

☆樱花仙子☆ 提交于 2019-12-08 01:32:29

问题


In my php script,

$names = $_GET['part'];
$result = mysql_query("SELECT * FROM  namestable where names LIKE'%$names%' LIMIT 10");
while ($row = mysql_fetch_assoc($result)) {
        $colors[]=$row['publishers'];
}

checks for matches and works well.

But suppose my table has a name Alfred, the suggestion will appear only if i type Alfr and not appearing if i type alfr


回答1:


The example you've provided will work if you're using a case insensitive collation such as utf8_general_ci. (See the MySQL Character Set Support manual section for more information.)

Alternatively, you could simply use the LOWER function as follows:

$names = strtolower(mysql_real_escape_string($_GET['part']));
$result = mysql_query("SELECT * FROM namestable WHERE names LIKE LOWER('%$names%') LIMIT 10");

Incidentally, if you're attempting to catch differences beyond simple case changes, you could also use MySQL's SOUNDEX function to obtain and find a match for strings that sound similar.

Incidentally, you need to use mysql_real_escape_string on your $names variable, or (better still) use prepared statements via the mysqli or PDO interfaces.



来源:https://stackoverflow.com/questions/4985026/php-mysql-jquery-ajax-autocomplete-case-sensitivity

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