Mysql 查询 LIKE 搜索并使用带符号的字符串

发布于 2025-01-01 17:57:59 字数 388 浏览 1 评论 0原文

我的问题是在搜索查询中使用符号。我希望用户能够使用符号而不会出现问题,但是 mysql 中的 LIKE 函数似乎不是解决方案,所以我需要一些帮助。

示例:如果有人搜索“Blue's car”并且“Blues car”在数据库中,则此查询将返回 0 个结果。或者反之亦然,如果有人搜索“Blues car”并且“Blue's car”在数据库中,则此查询也将返回 0 个结果。

这是我当前正在使用的示例:

("SELECT Title FROM MyData WHERE Title LIKE '%".$search."%'")

Is there another way on Provide better search results?

谢谢。

My problem is having symbols used in the search query. I want users to be able to use symbols without being a problem, but the LIKE function in mysql seems to not be the solution so I need some help.

EXAMPLE: If someone searches for "Blue's car" and "Blues car" is in the database, this query will return with 0 results. OR viseversa, if someone searches for "Blues car" and "Blue's car" is in the database, this query will return with 0 results as well.

This is an example of what I'm currently using:

("SELECT Title FROM MyData WHERE Title LIKE '%".$search."%'")

Is there another way on providing better search results?

Thanks.

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

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

发布评论

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

评论(4

川水往事 2025-01-08 17:57:59

搜索字段

您可以有一个额外的“搜索”字段,用于存储不带标点符号的标题,并且当用户输入搜索字符串时,在应用查询之前删除标点符号。您还可以删除前导“The”。

我将进一步解释上面的内容。

在数据库中你有这样的记录。 SearchTitle 是删除了前导“The”和标点符号的标题:

Title                  SearchTitle
------------------------------------------
The Blue's Clues       Blues Clues

在这里,用户知道确切的标题并输入以下搜索字符串:

The Blue's Clues

您去掉前导“The”和标点符号,得到以下结果:

Blues Clues

您的查询看起来像 在这里

SELECT Title FROM MyData WHERE SearchTitle LIKE '%Blues Clues%'

,用户不知道确切的标题并输入以下内容:

Blues Clues

去掉前导的“The”和标点符号,仍然会产生相同的结果:

Blues Clues

您的查询保持不变,并与搜索字段中的内容匹配:

SELECT Title FROM MyData WHERE SearchTitle LIKE '%Blues Clues%'

这可以是进一步改善。关键是对搜索字符串应用与搜索字段中存储的内容相同的规则。例如,将“two”和“II”转换为“2”等,删除“ON”和“AND”等其他单词。

全文搜索

MySQL 全文搜索使用“ 自然语言”搜索。我不确定这是否适合您的情况,但可能值得研究。

Search Field

You could have an additional "search" field, that stores the title without punctuation and when user enters search string, strip out punctuation before applying query. You could also remove leading "The".

I'll further explain the above.

In the database you have a record like this. The SearchTitle is the Title with the leading "The" and punctuation removed:

Title                  SearchTitle
------------------------------------------
The Blue's Clues       Blues Clues

Here, the user knows the exact title and enters the following search string:

The Blue's Clues

You strip out the leading "The" and the punctuation, yielding the following:

Blues Clues

Your query looks like this

SELECT Title FROM MyData WHERE SearchTitle LIKE '%Blues Clues%'

Here, the user doesn't know the exact title and enters the following:

Blues Clues

Stripping the leading "The" and the punctuation, still yields the same thing:

Blues Clues

Your query stays the same, and matches what's in the search field:

SELECT Title FROM MyData WHERE SearchTitle LIKE '%Blues Clues%'

This can be further improved. The key is to apply the same rules to the search string as to what's stored in the search field. For example, convert "two" and "II", to "2", etc., remove additional words like "ON" and "AND", etc.

Full-Text Searches

MySQL FullText searches use "Natural Language" searches. I'm not sure if this would do the trick in your case, but it might be worth researching.

微凉 2025-01-08 17:57:59

我不会说这一定更好,但 MySQL 有正则表达式支持。 REGEXP

I wouldn't say this is necessarily better but MySQL has Regular Expression support. REGEXP

暗地喜欢 2025-01-08 17:57:59

将任何用户输入直接传递给 SQL 调用会使您的代码可能容易受到 SQL 注入攻击,这是一种安全风险,而且您会发现它并不总是提供预期的结果。为了解决这个问题,建议开发人员使用准备好的语句或转义输入[有时称为清理输入]。有关更多信息,请参阅以下内容:什么是最好的方法使用 PHP 清理用户输入?

Passing in any user input directly to a SQL call leaves your code potentially vulnerable to SQL injection attacks, which is a security risk, as well as what you've found that it doesn't always provide the expected result. To fix this problem, developers are advised to use prepared statements or escape the inputs [sometimes called sanitizing the input]. See the following for some more information: What's the best method for sanitizing user input with PHP?

或十年 2025-01-08 17:57:59

您可以使用这个搜索词。这对于 MySQL 的多列搜索非常有用,

$q = htmlspecialchars($_GET["q"], ENT_QUOTES, "ISO-8859-1");
$q = stripslashes($q);
$search_exploded = explode (" ", $q);
$construct = "";

  $construct .=" CONCAT(title,' ',category,' ',subCategory,' ',description,' ',contact,' ',email,' ',website,' ',address) LIKE '%$q%' ";

foreach($search_exploded as $search_each)
{

$construct .=" OR CONCAT(title,' ',category,' ',subCategory,' ',description,' ',contact,' ',email,' ',website,' ',address) LIKE '%$search_each%' "; 

}  
$constructs ="SELECT * FROM content WHERE   active='1' AND ($construct) ";
$run = mysql_query($constructs);
$foundnum = mysql_num_rows($run);

如果您愿意,我将提供更多信息

You can you this search term. This is very useful for multi column search from MySQL

$q = htmlspecialchars($_GET["q"], ENT_QUOTES, "ISO-8859-1");
$q = stripslashes($q);
$search_exploded = explode (" ", $q);
$construct = "";

  $construct .=" CONCAT(title,' ',category,' ',subCategory,' ',description,' ',contact,' ',email,' ',website,' ',address) LIKE '%$q%' ";

foreach($search_exploded as $search_each)
{

$construct .=" OR CONCAT(title,' ',category,' ',subCategory,' ',description,' ',contact,' ',email,' ',website,' ',address) LIKE '%$search_each%' "; 

}  
$constructs ="SELECT * FROM content WHERE   active='1' AND ($construct) ";
$run = mysql_query($constructs);
$foundnum = mysql_num_rows($run);

If you want i will provide more information

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