gpt4 book ai didi

php - MySQL LIKE 查询不提取相似数据

转载 作者:太空宇宙 更新时间:2023-11-03 12:29:06 26 4
gpt4 key购买 nike

我有一个简单的基于 MySQL 的搜索脚本,下面我将只发布与问题相关的部分。

$name = 'John Doe';
$query = 'SELECT username FROM members WHERE name LIKE %$name%';

现在,问题是当我搜索 John Doe 而不是获取具有该特定名称的用户时,我获取了所有名为 JohnDoe< 的用户 而且,有趣的是,它甚至没有将 John Doe 放在顶部,这意味着......您可以在此之前找到 John xxxx

是否有高级 MySQL 属性来完成此任务?

最佳答案

改变

$query = 'SELECT username FROM members WHERE name LIKE %$name%';

$query = 'SELECT username FROM members WHERE name LIKE "'.%$name%.'"';

$query = "SELECT username FROM members WHERE name LIKE '%$name%'";

请注意,更改为

$query = 'SELECT username FROM members WHERE name LIKE "%$name%"';

不会做这个把戏。

这是因为single quoted strings不要解释变量。

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

Double quoted strings但是,会的。

The most important feature of double-quoted strings is the fact that variable names will be expanded. See string parsing for details.

更新:感谢 Memolition,这被大量编辑并改变了含义 :)

关于php - MySQL LIKE 查询不提取相似数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16286773/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com