有时mysql全文搜索不会返回任何结果

时间:2014-10-07 12:48:02

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

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

例如,我正在寻找作者'彼得施密特'。如果我搜索'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>'
    }

导致此问题的原因是什么?

1 个答案:

答案 0 :(得分:0)

如果你不知道syntax,请尽量不要使用BOOLEAN MODE匹配。同时检查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>'
}
?>