0

我的 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>'
    }

是什么导致了这个问题?

4

1 回答 1

0

如果您不知道它的语法,请尽量不要使用 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>'
}
?>
于 2014-10-07T13:41:36.390 回答