2014-10-07 101 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