两个单词和空格在使用 LIKE 的 MYSQL 查询中不起作用

发布于 2024-10-10 16:20:38 字数 409 浏览 3 评论 0原文

我正在为我的网站创建一个搜索器,这对于一个单词查询来说效果很好,

 $consulta = mysql_query("SELECT * FROM recetas WHERE (titulo LIKE '%$busqueda%' OR intro LIKE '%$busqueda%' OR extra LIKE '%$busqueda%') ORDER BY id DESC");

但如果我输入“两个单词”,它不会给我结果,$busqueda它是来自<的结果通过 $_POST['TERM'] 给出的 ;input/>

知道我缺少什么吗?

已解决,

我缺少将变量编码为 URI...哎呀哈哈,谢谢!

i'm making a searcher for my site and this works fine with one word querys

 $consulta = mysql_query("SELECT * FROM recetas WHERE (titulo LIKE '%$busqueda%' OR intro LIKE '%$busqueda%' OR extra LIKE '%$busqueda%') ORDER BY id DESC");

But if i type 'two words' it doesn't give me result, $busqueda it's result from a <input/> given through $_POST['TERM']

any idea what i'm missing?

SOLVED

i was missing to encode the variable to URI... oops haha THANKS!

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

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

发布评论

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

评论(3

情绪少女 2024-10-17 16:20:38

想想你的查询最后会是什么样子:

Select ... where '%two words%. ...

如果你想搜索这样的单词,你必须调整数据使其看起来更像:

 ... Like '%two%words%'
 ... Like '%two%' or like '%words%'

取决于你的确切搜索要求

Think of how your query will look at the end:

Select ... where '%two words%. ...

If you want to search for words like that, you'll have to massage the data to look more like:

 ... Like '%two%words%'
 ... Like '%two%' or like '%words%'

depending on your exact search requirements

夜访吸血鬼 2024-10-17 16:20:38

除非这两个单词在文本中相邻,否则 LIKE 运算符不会找到它们。您可能需要使用全文搜索

为了找到两个不连续的单词,输入需要分成两个单独的值,并且查询必须如下所示:

WHERE (titulo LIKE '%$word1%' OR intro LIKE '%$word1%' OR extra LIKE '%$word1%' OR
       titulo LIKE '%$word2%' OR intro LIKE '%$word2%' OR extra LIKE '%$word2%' )

假设您想要与任一单词匹配。如果两者必须匹配,那么就像这样:

WHERE (titulo LIKE '%$word1%' AND titulo like '%$word2%' OR
       intro LIKE '%$word1%' AND intro LIKE '%$word2%'  OR 
       extra LIKE '%$word1%' AND extra LIKE '%$word2%' )

还有一件事。最好使用参数化查询来避免 SQL 注入

Unless the two words are adjacent in the text, the LIKE operator won't find them. You may want to use full text search.

In order to find two non-contiguous words, the input would need to be split up into two separate values and the query would have to look something like this:

WHERE (titulo LIKE '%$word1%' OR intro LIKE '%$word1%' OR extra LIKE '%$word1%' OR
       titulo LIKE '%$word2%' OR intro LIKE '%$word2%' OR extra LIKE '%$word2%' )

That is assuming you want a match with either word. If both must match, then something like this:

WHERE (titulo LIKE '%$word1%' AND titulo like '%$word2%' OR
       intro LIKE '%$word1%' AND intro LIKE '%$word2%'  OR 
       extra LIKE '%$word1%' AND extra LIKE '%$word2%' )

And one other thing. It would be better to use parameterized queries to avoid an SQL injection.

遮云壑 2024-10-17 16:20:38

其中 ( MATCH( 标题、介绍、额外) 反对 (布尔模式中的“word1 word2”))

where ( MATCH( titulo, intro, extra) AGAINST ('word1 word2' in BOOLEAN MODE))

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