php mysql jquery AJAX 自动完成区分大小写

发布于 2024-10-17 13:58:18 字数 357 浏览 15 评论 0原文

在我的 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'];
}

检查匹配并且运行良好。

但是假设我的表有一个名字 Alfred,只有当我输入 Alfr 时才会出现该建议,而如果我输入 alfr 则不会出现该建议

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

握住你手 2024-10-24 13:58:18

如果您使用不区分大小写的排序规则(例如 utf8_general_ci),您提供的示例将起作用。 (有关详细信息,请参阅 MySQL 字符集支持手册部分。 )

或者,您可以简单地使用 LOWER函数如下:

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

顺便说一句,如果您试图捕获简单大小写更改之外的差异,您还可以使用 MySQL 的 SOUNDEX 函数获取并查找听起来相似的字符串的匹配项。

顺便说一句,您需要在 $names 变量上使用 mysql_real_escape_string ,或者(更好)通过 mysqli 或 PDO 接口使用准备好的语句。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文