phpandroidfull-text-searchsearch-engineboolean-search

Fulltext Search engine, multiple columns, boolean mode


I am making a search engine for an android app that does fulltext search and match against multiple columns against '+word1 +word2' in boolean mode. However, I can't get any search result.

E.g. search field type- "open sea" then, Sql will search Match...Against ('+open +sea' IN BOOLEAN MODE)

and display list of selectable results, on which each result clicked, will provide details of the particular result on a new page.

Sorry, I am a newbie in android app development.

Here is my php code for search.php

<?php

 @ $db = new mysqli('localhost','username','password','db');

  if (mysqli_connect_errno()) {
     echo 'Error: Could not connect to database.  
     Please try again later.';
     exit;
  }

if(!empty($_POST)){

     $term = $_POST['query'];

    $words = explode(" ", trim($term));
    $termArray = array();
    foreach($words as $word){
        if(!empty($word)){
            $termArray[] = "+$word";
                         }
                }
    $searchquery = implode(" ", $termArray);

  if (!$term) {
     echo 'You have not entered any search details. Please go back and try again.';
     exit;
    }
    //initial query

    $query = "SELECT * 
                FROM servicetable 
                    WHERE MATCH(title,cat,brand,company) 
                        AGAINST ('".$searchquery."' IN BOOLEAN MODE)
                            ORDER BY title ASC";

    $result = $db->$query;
    $num_results = $result->num_rows;

    //show user what user searched.   
    echo $searchquery;

    echo "<p>Results found: ".$num_results."</p>";

   //counts results.    
   if ($num_results == 0)    
   {    
      echo "Sorry, but we can not find an entry to match your query<br><br>";    
   }    

  for ($i=0; $i <$num_results; $i++) {
     $row = $result->fetch_assoc();
     echo "<p><strong>".($i+1).". Outlet Name: ";
     echo stripslashes($row['title']);

     echo "</strong><br />Category: ";
     echo stripslashes($row['cat']);
     echo "<br />Opening Hours: ";
     echo stripslashes($row['ophours']);
     echo "<br />Brand: ";
     echo stripslashes($row['brand']);
          echo "</strong><br />Company: ";
     echo stripslashes($row['company']);
     echo "</p>";
  }

  $result->free();
  $db->close();
} else {
?>
        <h1>Search</h1> 
        <form name="form1" action="search.php" method="post"> 
            Enter Search:<br /> 
            <input type="text" name="query" id="query" placeholder="Search a service"/> 
                        <br/>
            <input type="submit" value="Search Now" name="completedsearch"  /> 
        </form> 

    <?php
}

?> 

Solution

  • Hi I have found my error:

    this line correction-->

    $result = $db->query($query);