Search code examples
phpmysqlfull-text-searchsearch-engine

Assign search results into a variable - PHP Fulltext Search Boolean


I created a Fulltext search in Boolean Mode using PHP/MySQL. It runs perfectly, with relevance ranking and index assigned to concerned database fields. The database contains two tables:

business -> name, description, contact, address, serialid

*search_terms* -> terms, querytime, date_searched, results.

Then now, I want to get all the search results and assigned it to a variable ($results). This $result will be stored to the search_term table along with terms, querytime and date_searched.

Here is my code (without $result)

function search($term){
    $term = mysql_real_escape_string($term);
    $startTime = microtime(true);
    $query = mysql_query("SELECT *, MATCH (Name) AGAINST ('+$term*' IN BOOLEAN MODE) AS rel1, MATCH (Description) AGAINST ('+$term*' IN BOOLEAN MODE) AS rel2, MATCH (Keywords) AGAINST ('+$term*' IN BOOLEAN MODE) AS rel3 FROM business WHERE MATCH (Name,Description,Keywords) AGAINST ('+$term*' IN BOOLEAN MODE) ORDER BY (rel1*0.60)+(rel2*0.25)+(rel3*0.15) DESC") or die(mysql_error());
    $endTime = microtime(true);
    $queryTime = substr($endTime - $startTime, 0,6);
    if(mysql_num_rows($query) == 0){
        echo "<p>No results found for <i>". $term ."</i></p>";
    }
    else{
        while($row = mysql_fetch_assoc($query)){
            echo "<h4><a href='viewBusiness.php?serial=" . $row['SerialId'] . "'>" . $row['Name'] . "</a></h4>";
            $desc = substr($row['Description'], 0,100);
            $score = $row['rel1'] + $row['rel2'] + $row['rel3'];
            echo "<p>" . $desc .". . .</p>";
        }
        $numOfResult = mysql_num_rows($query);
        echo "<hr/><p><b>" . $numOfResult ." </b>result(s) found within " . $queryTime . " seconds.</p>";
        $ip = $_SERVER['REMOTE_ADDR'];
        $query2 = mysql_query("INSERT INTO search_term(Term, QueryTime, Ip) VALUES('$term', '$queryTime', '$ip')") or die(mysql_error());
    }
}

I am novice in PHP and this is my first application. Thank you very much for the help!


Solution

  • You can create and save your $result to DB like this:

    /* Your code before the cycle... */
    
    $result = array(); /* The array where to store results */
    while($row = mysql_fetch_assoc($query)) {
        /* Your code for printing, just as posted... */
        $result[] = $row; /* Store the result row in the array */
    }
    
    /* The rest of your code, before second query... */
    
    /* Serialize the result data and save it to database */
    $result_serialized = mysql_real_escape_string(serialize($result));
    $query2 = mysql_query("
        INSERT INTO search_term(Term, QueryTime, Ip, result) 
        VALUES('$term', '$queryTime', '$ip', '$result_serialized')
    ") or die(mysql_error());
    

    But be careful: $result (and so $result_serialized) can be very very big depending on results type and number. Be sure you can handle it. The result field in the search_term DB table must be a TEXT or a bigger string data type column.

    And read about serialize() and unserialize()!