【问题标题】:sometimes mysql fulltext search doesn't return any results when it should be有时mysql全文搜索不应该返回任何结果
【发布时间】:2014-12-01 21:40:22
【问题描述】:

我的 MySQL 数据库表包含 2510 条记录。当我尝试使用全文搜索在列中搜索字符串时,有时我没有得到任何结果。只是一个空的 html 表。

例如,我正在搜索作者“Peter Schmidt”。如果我搜索“Peter”,我会找到正确的作者,但如果我搜索“Schmidt”,html 表会显示其他作者,但不是正确的作者。作者的专栏由“姓,名”(施密特,彼得)组成。

这是我的一段代码:

    $author = mysql_real_escape_string($_GET['author']);
    $sql = "SELECT * FROM books WHERE MATCH(author) AGAINST ('$author' IN BOOLEAN MODE)";
    $query = mysql_query($sql);
    if (!$query) {
        echo 'We cannot find the author you are searching for. Please try again.';
        echo '<a href="'.$_SERVER['PHP_SELF'].'" class="back" style="margin:0;" title="Go back">&raquo; Go back</a>';

    } else {
        echo '<p>These authors match your query:</p><table>'
        while ($result = mysql_fetch_array($query)) {
                echo '<tr><td>'.$result['author'].'</td></tr>';
        }
        echo '</table>'
    }

是什么导致了这个问题?

【问题讨论】:

  • 当您在搜索查询中输入Schmidt, 时会发生什么?想知道逗号是否妨碍您。
  • 我仍然没有找到合适的作者,只是其他人。
  • 试试 'like' 运算符
  • 我认为问题在于逗号。只需尝试在 $autor: AGAINST ('$author*' IN BOOLEAN MODE) 后添加 * 看看会发生什么
  • 好的,下一个问题是,Schmidt 是否出现在超过一半的记录中。我不认为它会,但我不得不问。

标签: php mysql sql full-text-search match-against


【解决方案1】:

如果您不知道syntax,请尽量不要使用布尔模式匹配。还要检查 php/mysql 连接中的正确编码:

<?php
mb_internal_encoding("UTF-8");
mysql_query("SET character_set_client = utf8;");
mysql_query("SET character_set_results = utf8;");
mysql_query("SET collation_connection = utf8_general_ci;");
$author = mysql_real_escape_string($_GET['author']);
$sql = "SELECT * FROM books WHERE author LIKE '%$author%'";
$query = mysql_query($sql);
if (!$query) {
    echo 'We cannot find the author you are searching for. Please try again.';
    echo '<a href="'.$_SERVER['PHP_SELF'].'" class="back" style="margin:0;" title="Go back">&raquo; Go back</a>';
} else {
    echo '<p>These authors match your query:</p><table>'
    while ($result = mysql_fetch_assoc($query)) {
            echo '<tr><td>'.$result['author'].'</td></tr>';
    }
    echo '</table>'
}
?>

【讨论】:

  • 我尝试过使用like运算符,但没有任何区别。
  • 请使用命令转储表信息:mysql> show table status where Name='table_name';
  • 所以,这个表的排序规则是 utf8_general_ci。请检查您是否已将 php 定义为使用 utf8 - 添加 mb_internal_encoding("UTF-8");某处开始。同时检查 mysql 连接是否正确排序。
  • 名称:书籍,引擎:MyISAM,版本:10,Row_format:动态,行:2510,Avg_row_length:233,Data_length:586680,Max_data_length:281474976710655,Index_length:194560,Data_free:0,Auto_increment: 2512,排序规则:utf8_general_ci
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多