Philip Tiong
Philip Tiong

Reputation: 113

Empty search result when trying to create PHP search script

why when i try to search mysql database, it shows 0 result? i altered the table into FULLTEXT etc properly. What am I missing? I have put the very basic search exercise code down here. I want to search into my table 'products' and extract the details from there. but no luck

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
    $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
    if($_POST['filter1'] == "Whole Site"){
        $sqlCommand = "SELECT product_name AS name, details AS details FROM products WHERE MATCH (product_name ,details) AGAINST ('$searchquery' IN BOOLEAN MODE)";
    } 
    require_once("storescripts/connect_to_mysqli.php");
    $query = mysqli_query($myConnection,$sqlCommand) or die(mysqli_error($myConnection));
    $count = mysqli_num_rows($query);
    if($count > 1){
        $search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />$sqlCommand<hr />";
        while($row = mysqli_fetch_array($query)){

            $name = $row["name"];
                    $details= $row["details"];
            $search_output .= "Name: '.$name.' - '.$details.'<br />";
        } // close while
    } else {
        $search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
    }
}
?>
<html>
<head>
</head>
<body>
<h2>Search the Exercise Tables</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Search For: 
  <input name="searchquery" type="text" size="44" maxlength="88"> 
Within: 
<select name="filter1">
<option value="Whole Site">Whole Site</option>

</select>
<input name="myBtn" type="submit">
<br />
</form>
<div>
<?php echo $search_output; ?>
</div>
</body>
</html>

Upvotes: 0

Views: 80

Answers (2)

Rahul
Rahul

Reputation: 1181

Problem is there with your if part as:

  1. replace your current :

    if($_POST['filter1'] == "Whole Site") { $sqlCommand = "SELECT product_name AS name, details AS details FROM products WHERE MATCH (product_name ,details) AGAINST ({$searchquery} IN BOOLEAN MODE)"; }

  2. In case it couldnt get to query due to if statement add:

    else { echo "No seqrch query found"; }

hope it works now

Upvotes: 0

Momo1987
Momo1987

Reputation: 544

Change this:

$sqlCommand = "SELECT product_name AS name, details AS details FROM products WHERE MATCH (product_name ,details) AGAINST ('".$searchquery."' IN BOOLEAN MODE)";

i have changed

AGAINST ('$searchquery' IN

to

AGAINST ('".$searchquery."' IN 

Upvotes: 3

Related Questions