Jump to content


Search engine not working properly

  • Please log in to reply
3 replies to this topic

#1 Truz

  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 21 August 2006 - 05:05 PM

Hey all I found this Tutorial http://www.devpapers.com/article/306 which show a really easy way to do a database search engine and it works perfectly....well maybe not that "perfectly" ;D.
The problem is that when I search Names of people in my database it gets all messed up since it returns the same name Repeated for each part of the name searched so I was Wondering if anyone could help me I’ve made an example you can find it here http://oskulostudios...archexample.php just type "Rodrigo" in the search box to see how it does work if you search only one word at the time and then search for "Rodrigo Example" to see the bug that returns the same database name twice for each name like this:

-Rodrigo Example

-Rodrigo Example

And if you add another word to the search like "Rodrigo Example 3" it returns each record three times.
I'm also pasting the example code below. Thanks I appreciate any replies.


$limit = 5; 

  $var = @$_GET['q'] ; 
  $trimmed = trim($var);   
  $trimmed_array = explode(" ",$trimmed);  

if ($trimmed == "") { 
  $resultmsg =  "<p>Search Error</p><p>Please enter a search...</p>" ; 

if (!isset($var)){ 
  $resultmsg =  "<p>Search Error</p><p>We don't seem to have a search parameter! </p>" ; 
foreach ($trimmed_array as $trimm){ 
     $query = "SELECT * FROM table WHERE user LIKE \"%$trimm%\" OR name LIKE  \"%$trimm%\" ORDER BY user DESC" ;  

     $numresults=mysql_query ($query); 
     $row_num_links_main =mysql_num_rows ($numresults); 

     if (empty($s)) { 

      $query .= " LIMIT $s,$limit" ; 
      $numresults = mysql_query ($query) or die ( "Couldn't execute query" ); 
      $row= mysql_fetch_array ($numresults); 

      //This is supposed to store record id of every item that contains the keyword in the array to avoid display of duplicate search result. 
          $adid_array[] = $row[ 'userno' ]; 
      }while( $row= mysql_fetch_array($numresults)); 
 } //end foreach 

if($row_num_links_main == 0 && $row_set_num == 0){ 
   $resultmsg = "<p>Search results for:" . $trimmed  ."</p><p>Sorry, your search returned zero results</p>" ; 
   //delete duplicate record id's from the array. To do this we will use array_unique function 
   $tmparr = array_unique($adid_array);  
   foreach ($tmparr as $v) {  
       $newarr[$i] = $v;  


<form action="searchexample.php" method="get" name="search"> 
  <div align="center"> 
      <input name="q" type="text" value=" <?php echo $q; ?> " size="15">  
      <input name="search" type="submit" value="Search"> 

 if( isset ($resultmsg)){ 
  echo $resultmsg; 
  echo "Search results for: " . $var; 
foreach($newarr as $value){ 
$query_value = "SELECT * FROM table WHERE userno = '$value'"; 
 $num_value=mysql_query ($query_value); 
 $row_linkcat= mysql_fetch_array ($num_value); 
 $row_num_links= mysql_num_rows ($num_value); 

 $row_linkcat[ 'field1' ] 
  $titlehigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'user' ] ); 
  $linkhigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'nombre' ] ); 

foreach($trimmed_array as $trimm){ 
    if($trimm != 'b' ){ 
        $titlehigh = preg_replace( "'($trimm)'si" ,  "<b>\\1</b>" , $titlehigh); 
        $linkhigh = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $linkhigh);  

<?php echo $titlehigh; ?><br> 
<?php echo $linkhigh; ?><br> 
}   //end foreach $trimmed_array  
}  //end foreach $newarr 
   if($row_num_links_main > $limit){ 
   // next we need to do the links to other search result pages 
      if ($s>=1) { // do not display previous link if 's' is '0' 
         echo "<div align='left'><a href='$PHP_SELF?s=$prevs&q=$var&catid=$catid'>Previous " .$limit. "</a></div>"; 
     // check to see if last page 
     $slimit =$s+$limit; 
       if (!($slimit >= $row_num_links_main) && $row_num_links_main!=1) { 
     // not last page so display next link 
           echo "<div align='right'><a href='$PHP_SELF?s=$n&q=$var&catid=$catid'>Next " .$limit. "</a></div>"; 


#2 shocker-z

  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 21 August 2006 - 06:21 PM


echo "Search results for: " . $var;


echo "Search results for: " . $trimm;

as $var is the whole variable $_GET['q'] and by the looks of it what you want is just

Search results for: Rodrigo
Search results for: Example

Which using $trimm holds :)

www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 Truz

  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 21 August 2006 - 07:10 PM

Hey shocker-z thanks for the reply but that didn't work since the problem is not in the "search results for:" part cause that only echoes the same text that you searched for, that is ok, the problem is that for some reason every word you add in the search sends back the information about the same person repeated the same number of times as the words in the search. For example let say you search for James Summers so in the results you'll get


Search results for: James Summers

-Name: James Summers

-Name: James Summers

-Name: Peter Summers

-Name: Peter Summers

As you can see it returns not only the exact matching words twice (or as many times as words) but also it returns other registries that have one of the searched words twice (or as many times as words) ???.
Hope this make the problem clearer thanks

#4 Truz

  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 24 August 2006 - 07:03 PM

mmmmmmmm..... guess no one can help me  :'(

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users