duai4512 2014-10-07 12:48
浏览 46
已采纳

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

My MySQL database table contains 2510 records. When I try to search for a string in a column, using fulltext search, sometimes, I don't get any results. Just an empty html table.

For example, I'm searching for the author 'Peter Schmidt'. If I search for 'Peter' I'll get the right author, but if I search for 'Schmidt', the html table shows other authors, but not the right one. The author's column consist of 'surname, name' (Schmidt, Peter).

This is a piece of my code:

    $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>'
    }

What causes this issue?

  • 写回答

1条回答 默认 最新

  • ds753947 2014-10-07 13:41
    关注

    Try not to use BOOLEAN MODE matching if you dont know syntax for it. As also check for proper encoding in php/mysql connection:

    <?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>'
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码