0

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;
}
thanksd
  • 54,176
  • 22
  • 157
  • 150
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Feb 05 '16 at 13:47
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Feb 05 '16 at 13:47
  • i need to work with the old php because my host provider is not yet upgraded to php 7. ty for comment – user2052643 Feb 05 '16 at 13:48
  • brackets are for subqueries; remove them. and all you're doing is `print results`?? what ARE those exactly? – Funk Forty Niner Feb 05 '16 at 13:49
  • i removed the content of "echo" for reasons... its simple $results[title] and $results[description] tags – user2052643 Feb 05 '16 at 13:51
  • unless `John` is present inside all your columns, it will always return one result. – Funk Forty Niner Feb 05 '16 at 13:54
  • to look not only 1 word or multiple, then look for what? – Mark Ng Feb 05 '16 at 14:00
  • You do not need PHP7 to work with MySQLi or PDO – Jay Blanchard Feb 05 '16 at 14:03

1 Answers1

1

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;
} ?>
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Alpesh Panchal
  • 1,723
  • 12
  • 9