Php Search Needs fixing

Status
Not open for further replies.

zxrcnew

Beta member
Messages
2
On my website I am try to creat a search engine but I can't get
multiple keywords to work

in the search engine "dubleeble" or "games" works but not "dubleeble games"

this is the code I am using
PHP:
<?php
 include 'db.inc.php';

 function search_result($keywords) {
   $returned_results = array();
   $where = "";
   $keywords = preg_split('/[\s]+/', $keywords);
   $total_keywords = count($keywords);
   foreach($keywords as $key=>$keyword) {
     $where .= "`keywords` LIKE '%$keyword%'";
     if ($key != ($total_keywords - 1))  {
       $where .= " AND ";
     }
   }
 $results = "SELECT `title`, LEFT(`description`, 70) as `description`, `url` FROM `articles` WHERE $where";
 $results_num = ($results = mysql_query($results)) ? mysql_num_rows($results): 0 ;
 if ($results_num === 0) {
      return false;
  } else {
  while ($results_row = mysql_fetch_assoc($results)) {
    $returned_results[] = array(
                         'title' => $results_row['title'],
                         'description' => $results_row['description'],
                         'url' => $results_row['url']
                         
    );
  }
  return $returned_results;
 }
 }
?>
 
OK well there are a few of things:
1) your search result will become smaller the more terms you put into it because you are using AND
2) I don't know why but it looks like you are using backticks around the attribute names, this isn't necessary
3) it's been a while since I did PHP but I think you might need to be a bit more careful about SQL injection
 
Status
Not open for further replies.
Back
Top Bottom