1

I have created search engine so when someone type in search box it takes data from my database and show the results in images.

Everything is working fine but if I have data in database with Name like 'Lamborghini', when someone type in search box 'lambo' or 'Lamborghini' it is working fine and show the result but when he types 'Lamborghini car' it is not showing any result.

Here is my PHP Code:

<html>
<link href="css/imgpages.css" rel="stylesheet" type="text/css">

    <head>
        <title>Search the Database</title>
    </head>

    <body>

    <form action="" method="post">
     Search: <input type="text" name="term" /><br />
    <input type="submit" name="submit" value="Submit" />
    </form>



    </body>
 </html>

 <?php
include("connection.php");

$term = $_POST['term'];


$query = mysql_query("select * from save_data where Title like '%$term%'");


while($row = mysql_fetch_array($query)) {

    $post_id = $row['ID'];
    $title = $row['Title'];
    $image = $row['Name'];

?>
    <div id="body">

    <a href="pictures.php?title=<?php echo $title; ?>">
    <div id="title"><?php echo $title; ?></div></a>

    <a href="pictures.php?title=<?php echo $title; ?>">
    <img  src='uploads/<?php echo $image; ?>' width='140' height='140'></a>
    </div>

<?php } ?>
tshepang
  • 12,111
  • 21
  • 91
  • 136
user3049358
  • 15
  • 1
  • 7
  • 3
    You should really do something about your SQL-injection vulnerability. Use either the `mysqli` or `PDO` extension when interacting with the database. You can read more here: http://www.php.net/manual/en/intro.mysql.php. – display-name-is-missing Nov 30 '13 at 12:46
  • 1
    That is probably because you don't have a title in your database that has "Lamborghini car" in it so it won't find it. – putvande Nov 30 '13 at 12:48
  • But in Google if you write this it will find it even it has no result with this name @putvande – user3049358 Nov 30 '13 at 12:51
  • Well just separate the words using `explode()` and run a search matching _any_ of the words (so an `OR` operator). – arkascha Nov 30 '13 at 12:51
  • 1
    Yes but Google uses more than just `like '%$term%'` – putvande Nov 30 '13 at 12:51
  • 3
    Consider using a full text search system, like Zend Lucene, or perhaps full text matching in MySQL. A `LIKE` statement will only do trivial matching (e.g. if keywords are reversed, no match will be found). – halfer Nov 30 '13 at 12:52
  • @putvande Haha, yeah, a little bit more than that :) – Shomz Nov 30 '13 at 12:52
  • 2
    Where is search engine? – Lion Nov 30 '13 at 12:53

4 Answers4

2

You should:

  • fix your SQL (make it secure - just one search could delete your whole database if you leave it like this, switch to mysqli or PDO because mysql_* functions are deprecated)
  • split your query string into separate words and decide how to handle them (whether you'll use OR or AND... in other words, do all search terms need to match, or any of them, etc.)

When you deal with all that and decide to make it more advanced, you should learn about full-text searching.

Shomz
  • 37,421
  • 4
  • 57
  • 85
0

It is 'cause { %$term% } means the query try search something that contain all of the string in $term. Try this :

$term = explode(" ",$term);
if (count($term) > 0) {
   $Where = '';
   foreach($term as $Item) {
      $Where .= "Title like '%$Item%' OR ";
   }
   $Where = substr($Where,0,-4);
   $query = mysql_query("SELECT * FROM `save_data` WHERE $Where");
}

In this way the search will check out all words.

Pain67
  • 326
  • 1
  • 9
0

As the above people said just Explode the input string into an array and search for documents(description, keywords) where most of the elements match. Also take care of SQL Injection.

sathyam1992
  • 145
  • 1
  • 3
  • 13
-1

first make your term search explode so it would go into an array

<?php
      $term =  $_POST['term'];
      $words = explode(" ", $term);
?>

then create a forearch loop for the mysql_query of your search and implode with "OR"

<?php
     foreach ($words as $words)
       { 
          $queries[]="select * from save_data where Title LIKE '%" .                    
          mysql_real_escape_string($words) . "%'";
       }

     $query=implode(' OR ' ,$queries);

    $results=mysql_query($query);

    while($row = mysql_fetch_array($results)) 
    {
       $post_id = $row['ID'];
       $title = $row['Title'];
       $image = $row['Name'];
 ?>

    <div id="body">
        <a href="pictures.php?title=<?php echo $title; ?>">
            <div id="title"><?php echo $title; ?></div>
        </a>
       <a href="pictures.php?title=<?php echo $title; ?>">
            <img  src='uploads/<?php echo $image; ?>' width='140' height='140'>
       </a>
   </div>

 <?php }   ?>