2014-10-07 106 views
0

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

例如,我正在寻找作者'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>' 
    } 

是什么原因导致这个问题?

+0

当您在搜索查询中键入'Schmidt'时会发生什么?想知道这个逗号是否会妨碍你。 – Crackertastic 2014-10-07 12:52:46

+0

我仍然没有找到正确的作者,只是其他人。 – Steven 2014-10-07 12:53:51

+0

尝试'喜欢'运算符 – Srikanta 2014-10-07 12:53:58

回答

0

如果您不知道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>' 
} 
?> 
+0

我试过使用like运算符,但它没有区别。 – Steven 2014-10-07 13:47:09

+0

请使用以下命令转储表信息:mysql> show table status其中Name ='table_name'; – 2014-10-07 13:51:14

+0

因此,此表的排序规则是utf8_general_ci。请检查你已经定义的php使用utf8 - 添加mb_internal_encoding(“UTF-8”);在开始的某个地方。同时也检查mysql连接是否正确整理。 – 2014-10-07 14:03:03