user2052643
user2052643

Reputation: 13

How to apply multy word search in mysql

I need some points on how to make my php script to look not only 1 word or multiple like ( John Doe ). Currently my script only will find the 1st word "John" from the pointed table. Here is my script:

$query = $_GET['query'];   
$min_length = 1;

if (strlen($query) >= $min_length) { 
    $query = htmlspecialchars($query); 
    $query = mysql_real_escape_string($query);

    $raw_results = mysql_query(
        "SELECT * FROM filmi WHERE  (`title` LIKE '%".$query."%') 
         OR (`description` LIKE '%".$query."%') OR (`nomer` LIKE '%".$query."%')"
    ) or die(mysql_error());

    if (mysql_num_rows($raw_results) > 0) { 
        while ($results = mysql_fetch_array($raw_results)) {
            echo "


            print results       

            ";
        }
    } else { 
        echo "No results";
    }
} else { 
    echo "Minimum length is " . $min_length;
}

Upvotes: 0

Views: 56

Answers (1)

Alpesh Panchal
Alpesh Panchal

Reputation: 1713

Use this code, I have added new line.

This basically added wild cards % in place of space so that it will modify existing query and adds wildcard to it. This way it can find all records containing all words in the given search query.

<?php
$query = $_GET['query']; 

$min_length = 1;

if(strlen($query) >= $min_length){
    $query = htmlspecialchars($query); 
    $query = mysql_real_escape_string($query);
    //added new line ################
    $query = str_replace(' ', '%', $query);
    $raw_results = mysql_query("SELECT * FROM filmi
        WHERE  (`title` LIKE '%".$query."%') OR (`description` LIKE '%".$query."%') OR (`nomer` LIKE '%".$query."%')") or die(mysql_error());

    if(mysql_num_rows($raw_results) > 0){ 
        while($results = mysql_fetch_array($raw_results)){
            echo "
            print results       
            ";
        }
    }
    else{ 
        echo "No results";
    }
}
else{ 
    echo "Minimum length is ".$min_length;
} ?>

Upvotes: 1

Related Questions