PHP的MySQL jQuery的AJAX自动完成大小写 [英] php mysql jquery AJAX autocomplete case sensitivity
问题描述
在我的PHP脚本,
$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.
但是假设我的表都有一个名字阿尔弗雷德,如果键入 Alfr
和建议才会出现,如果我键入未出现 alfr
But suppose my table has a name Alfred, the suggestion will appear only if i type Alfr
and not appearing if i type alfr
推荐答案
您提供将工作,如果您使用的是不区分大小写的排序规则,如 utf8_general_ci
的例子。 (见MySQL的字符集支持手册部分获取更多信息。 )
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.)
另外,你可以简单地使用 LOWER 一>功能如下:
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");
顺便说一句,如果您正在试图追赶超越了简单的情况下改变的差异,你也可以使用MySQL的的 SOUNDEX 函数来获取并找到一个匹配那些听起来相似的字符串。
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.
顺便说一句,你需要用你的名字$变量mysql_real_escape_string,或者更好,是使用prepared通过的mysqli或PDO接口的声明。
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.
这篇关于PHP的MySQL jQuery的AJAX自动完成大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!