Jump to content

[SOLVED] Search doesn't return multiple entries


graham23s

Recommended Posts

Hi Guys,

 

with the search the way i have coded it, it won't bring back multiple entries if i search for members say:

 

graham23s

 

it brings the result back fine if 2 members join sat

 

graham23s

graham24s

 

it says no entries found, it then no longer gets the initial first entry (even though i loop the results out)

 

heres the search code:

 

<?php
    ## search trailers ##################################################################
    if ($_GET['action'] == "search") {
    
    
      if(isset($_POST['search'])) {
      
      echo '<br /><center><h4>Search Results</h4></center>';
      
      ## now do the search ##############################################################
      $keywords = CleanPosts($_POST['keywords'], 1);
      
      if(empty($keywords)) {
     
        echo '<br /><font color="red" /><b>Error:</font> Sorry, You Never Typed In A String To Search For!<br /><br />';
        include("includes/footer.php");
        exit;
     
      }      
      
     ## now do the search.../////////////////////////////////////////////////////////////
     $search_query = "SELECT * FROM `trailers` WHERE (`name` LIKE '%$keywords%')";
     $search_result = mysql_query($search_query) or die (mysql_error()); 
      
      ## any results ####################################################################
     if(mysql_num_rows($search_result) != 1) {
     
     echo 'Sorry, We Found No Search Results For (<font color="red">'.$keywords.'</font>)<br /><br />';
     include("includes/footer.php");
     exit;
     
     } else {
     
     ## echo table ######################################################################
     echo 'Displaying Search Results For (<i><tt><font color="red" /><b>' .$keywords. '</b></font></tt></i>)<br /><br />';
     echo '<table width="60%" border="1" bordercolor="#000000" cellpadding="3" cellspacing="0" />
           <tr>
           <th width="10%" bgcolor="#004E98"><font color="#ffffff">ID</font></th><th width="50%" bgcolor="#004E98"><font color="#ffffff">Movie Name</font></th>
           </tr>';
     
     while($row_search = mysql_fetch_array($search_result)) {
     
     $trail_id = $row_search['id'];
     $trail_name = $row_search['name'];
     $trail_link = $row_search['link'];
     
     echo '<tr><td>'.$trail_id.'</td><td><a href="'.$trail_link.'">'.$trail_name.'</a></td>';
     
     } // end while
     
     } 
     
        echo '</table><br />';
                  
        include("includes/footer.php");
        exit;
        
      } else {
?>

 

i have no idea whats goin on any help would be great

 

cheers

 

Graham

Entering "gra,ham" will not work as you query is looking for records containing that exact string somewhere in the name.

 

It would only work if the names were

 

gra,ham23s

gra,ham24s

 

Just entering "graham", or "ham" as I suggested, should find your records

Hi Barand,

 

yeah i see what you mean the thing is if only i was in the database say:

 

graham23s (if i searched graham i would get a result back)

 

if another user joined say:

 

graham24s (it would say no results even though there clearly is 2)

 

could it be somehting with the search query i have missed out?

 

thanks mate

 

Graham

no, it's something wrong with your conditional:

 

if(mysql_num_rows($search_result) != 1) {

 

that will trigger an error if the number of rows return is anything other than 1.  this is true of any number greater than 1.  change your conditional to only trigger the error if the number of rows returned is 0:

 

if(mysql_num_rows($search_result) == 0) {

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.