moeseph
moeseph

Reputation: 7

PHP/MySQL Search not returning ALL results

So I have a form which searches through a list of companies to see if that particular company is registered on the site.

When the company name is three or less characters and I can see it is in the database the results wont show.

For example, if the company's name is "ABB Eutech" and the search is for ABB, then the search returns 0.

However if you search for Eutech then the search returns the result.

Here's the code I'm using:

    <?php 
        $out = '';
        if ((isset($_POST['companysearch-name'])) && ($_POST['companysearch-name']==NULL)) {
            $out .= '<h3>No matches. Please enter a company name.</h3>';
        } else if (isset($_POST['companysearch-name'])) {
            $queryString = htmlspecialchars($_POST['companysearch-name'],ENT_QUOTES);                   
            if(strlen($queryString) >0) {
                $query = $wpdb->get_results("SELECT DISTINCT (company_name) FROM wp_companies WHERE MATCH (company_name) AGAINST ('$queryString')");
                $searchStr = "%" . $queryString . "%";
                $searchStr = str_replace(" ","%",$searchStr);
                $numresults = count($query);
                if($query) {
                    $out .= '<div><ul>';
                    foreach($query as $result):

                    $out .= '<li>'.$result->company_name.'</li>';
                    endforeach;
                    $out .= '</ul></div>';
                    $out .= "<p>Is your company on this list? If YES, your company already has the scheme in place and you can setup a donation right now.</p>";
                    $out .= '<a class="button" href="/donate/"><span>Start Giving</span><b></b></a>';
                } else {
                    $out .= '<h3>No matches found.</h3>';
                    $out .= "<p>Can't find your employer? They may have only just signed up - <a href='/contact-us/'>contact us</a> and we'll check it out for you.</p>";
                }
            }
        }
    ?>

Any help greatly received! ...Be gentle, it's my first time.

Upvotes: 0

Views: 122

Answers (1)

rawb
rawb

Reputation: 2426

Words that contain three characters or less are omitted from a fulltext index in MySQL by default.

The minimum and maximum lengths of words to be indexed are defined by the ft_min_word_len and ft_max_word_len system variables. (See Section 5.1.3, “Server System Variables”.) The default minimum value is four characters; the default maximum is version dependent. If you change either value, you must rebuild your FULLTEXT indexes. For example, if you want three-character words to be searchable, you can set the ft_min_word_len variable by putting the following lines in an option file: [mysqld] ft_min_word_len=3

Then restart the server and rebuild your FULLTEXT indexes. Note particularly the remarks regarding myisamchk in the instructions following this list.

http://dev.mysql.com/doc/refman/5.0/en/fulltext-fine-tuning.html

Upvotes: 4

Related Questions