我的MySQL数据库表包含2510条记录。当我尝试使用全文搜索在列中搜索字符串时,有时不会得到任何结果。只是一个空的html表。
例如,我正在搜索作者"彼得·施密特"。如果我搜索"Peter",我会找到合适的作者,但如果我搜索了"Schmidt",html表会显示其他作者,但不是合适的作者。作者的专栏包括"姓,名"(Schmidt,Peter)。
这是我的一段代码:
$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">» 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>'
}
是什么导致了这个问题?
如果你不知道BOOLEAN模式匹配的语法,尽量不要使用它。同时检查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">» 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>'
}
?>