【问题标题】:Returning "sorry, no results found" on a search of MySQL Database在搜索 MySQL 数据库时返回“抱歉,未找到结果”
【发布时间】:2011-10-27 15:19:17
【问题描述】:

我目前有一个知识库搜索功能,可以过滤掉一组常用词(a、able、a​​bout 等),这样它们就不会在查询中发送。不幸的是,当有人只搜索其中一个常用词时,它什么也不返回(而当它没有被过滤和查询时,我有代码返回“抱歉,没有找到结果”并将搜索记录在数据库中)。

如果搜索过滤词但未找到结果,我需要对过滤词执行相同操作,但我不确定最好的方法。下面是过滤后的代码。

//Defines common words to be striped.
$commonWords = array('a','able', etc)
//Strips common words from being included as a search term
$searchterm = preg_replace("/\b(".implode('|',$commonWords).")\b/i",'',$searchterm);

if(mysql_num_rows($result) == 0) {
echo "<img src=\"images/oops.png\"> Sorry, no results found.  <br /><br /><div id=\"custformSubmit\">Chat Now</div>";
//Records missing search terms in a database
mysql_query("INSERT INTO quer (IP, Query) VALUES ('$ip','$search')") or die(mysql_error());;

我知道 if(mysql_num_rows($result) == 0) 不起作用,因为在提交时实际上没有搜索任何内容,但我不确定解决此问题的最佳方法。

编辑:此外,div id 'custformSubmit' 会打开一个聊天窗口,以便与代表聊天,这是必要的。

【问题讨论】:

    标签: php javascript mysql search


    【解决方案1】:

    您应该在过滤后修剪字符串并检查 if(empty($filteredstring)) 然后不要执行 sql 查询并打印消息以找到任何内容。

    如果您没有复杂的模式,使用 str_replace 比使用 preg 更快。您可以将常用词设置为数组的键,并将替换 ('') 设置为值,然后执行 str_replace(array_keys($filter), $filter, $string)

    【讨论】:

      【解决方案2】:

      您可以使用preg_replace 的替换计数器(请参阅documentation)并输出Sorry, no results found.,如果替换的次数== 原始查询中的单词数。

      可能有更好的方法来做到这一点,但这就是我可以通过提供的代码提出的建议。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-16
        • 1970-01-01
        • 2023-03-14
        • 2011-02-08
        • 2017-11-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多