0

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?

Steven
  • 21
  • 7

1 Answers1

0

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>'
}
?>
  • I've tried to use the like operator, but it makes no difference. – Steven Oct 07 '14 at 13:47
  • Please dump table information with command: mysql> show table status where Name='table_name'; – Jevgenijs Vaikulis Oct 07 '14 at 13:51
  • So, collation for this table is utf8_general_ci. Please check you have defined php to use utf8 - add mb_internal_encoding("UTF-8"); somewhere in beggining. As also check mysql connection for proper collation. – Jevgenijs Vaikulis Oct 07 '14 at 14:03
  • Name: books, Engine: MyISAM, Version: 10, Row_format: Dynamic, Rows: 2510, Avg_row_length: 233, Data_length: 586680, Max_data_length: 281474976710655, Index_length: 194560, Data_free: 0, Auto_increment: 2512, Collation: utf8_general_ci – Steven Oct 07 '14 at 14:06