Jump to content

Problem with search keywords


MDanz

Recommended Posts

when i search for one word, everything is fine... when i search for two i get this error

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/ustackc1/public_html/search.php on line 116

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT * FROM `Stacks` WHERE `keywords` LIKE '%slam%' OR `keywords` LIKE '%onli' at line 1

 

it's coming from this part of the code

 

  $construct .= " `keywords` LIKE '%" . mysql_real_escape_string($search_each) . "%'";

}else{                                         

    $construct .= " OR `keywords` LIKE '%" . mysql_real_escape_string($search_each) . "%'";

}

 

            $construct = "SELECT * FROM `Stacks` WHERE $construct";

 

 

whats wrong?

Link to comment
https://forums.phpfreaks.com/topic/171408-problem-with-search-keywords/
Share on other sites

 <?php

mysql_connect("localhost", "Master", "password");
mysql_select_db("db");


    $button = $_GET['submit'];
    $search = $_GET['search'];

    if(!$search){
       echo "<br>You didn't submit a keyword";
    }else{
       if(!isset($search) ||  strlen($search)<=2){
          echo "<br><font color=white><br>search term too short</font>";
       }else{
          echo "<br><br><font color=white><br>you searched for <b>$search</b></font><hr size='1'>";
       }

       //explode our search term
       $search_exploded = explode(" ",$search);
       foreach($search_exploded as $search_each){
          //construct query
          $x++;
          if($x==1){
    $construct .= " `keywords` LIKE '%" . mysql_real_escape_string($search_each) . "%'";
}else{                                          
    $construct .= " OR `keywords` LIKE '%" . mysql_real_escape_string($search_each) . "%'";
} 

             $construct = "SELECT * FROM `Stacks` WHERE $construct";
       }

       // How many items to show per page
       $limit = 10;

       $page = $_GET['page'];

       if($page){
          $start = ($page - 1) * $limit;
       }else{
          $start = 0;
       }

     $foundnum = mysql_num_rows(mysql_query($construct));
$construct = $construct.' LIMIT '.$start.' , '.$limit;
$run = mysql_query($construct);
       
       echo mysql_error();

        if ($foundnum==0){
          echo "<br><br><font color=white>No Stacks Found</font>";
       }else{
          echo "<font color=white>$foundnum <img src='http://www.u-stack.com/mini%20stack.jpg'> Found!</font><hr size='1'><p>";
          echo '<table '.$margin.'>';
         
          while ($runrows = mysql_fetch_assoc($run)){
             
             $id = $runrows['id'];
             $name = $runrows['name'];
             $image = $runrows['image'];
             $hyperlink = $runrows['hyperlink'];
             $currency = $runrows['currency'];
             $info = $runrows['info'];
             $type = $runrows['type'];
             $rating = $runrows['rating'];
             $keywords =  $runrows['keywords'];

             echo '<td><tr>';
             echo "<a href='$hyperlink'><img src='http://www.u-stack.com/Stack.jpg'></a>";
             echo "</tr><font color=white><strong>  $name - $info - $rating <a href='rate.php?id=$id'><img src='http://www.u-stack.com/rate.jpg'></a></font>

             <a href='http://www.u-stack.com/reply.php?reply=$name&submit=reply' style='text-decoration: none'; >Reply</a>
             </center><br><br><hr size='1'></strong></td>";

          }
          echo '</table>';
        }   

       // How many adjacent pages should be shown on each side?
       $adjacents = 3;
           
        // Your file name  (the name of this file)
       $targetpage = "search.php";

       if ($page == 0){
          $page = 1;
       }

       $prev = $page - 1;

       $next = $page + 1;

       $lastpage = ceil($foundnum/$limit);

       $lpm1 = $lastpage - 1;

       $pagination = "";
       if($lastpage > 1){
          $pagination .= "<div class=\"pagination\">";
          // Previous
          if ($page > 1){
             $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$prev\">« previous</a>";
          }else{
             $pagination .= "<span class=\"disabled\">« previous</span>";
          }

          // Pages

          if ($lastpage < (7 + ($adjacents * 2))){   
             for ($counter = 1; $counter <= $lastpage; $counter++){
                if ($counter == $page){
                   $pagination .= "<span class=\"current\">$counter</span>";
                }else{
                   $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$counter\">$counter</a>";
                }
             }
          }elseif($lastpage > (5 + ($adjacents * 2))){
             if($page < 1 + ($adjacents * 2)){
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){
                   if ($counter == $page){
                      $pagination .= "<span class=\"current\">$counter</span>";
                   }else{
                      $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$counter\">$counter</a>";
                   }
                }
             }

          $pagination .= "...";
          $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$lpm1\">$lpm1</a>";
          $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$lastpage\">$lastpage</a>";
       }elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)){
          $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=1\">1</a>";
          $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=2\">2</a>";
          $pagination .= "...";

          for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++){
             if ($counter == $page){
                $pagination .= "<span class=\"current\">$counter</span>";
             }else{
                $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$counter\">$counter</a>";
             }
          }
          $pagination .= "...";
          $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$lpm1\">$lpm1</a>";
          $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$lastpage\">$lastpage</a>";
        }else{
          $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=1\">1</a>";
          $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=2\">2</a>";
          $pagination .= "...";

          for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++){
             if ($counter == $page){
                $pagination .= "<span class=\"current\">$counter</span>";
             }else{
                $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$counter\">$counter</a>";
             }
          }
       }
               
       // Next
       if ($page < $counter - 1){
           $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$next\">next »</a>";
       }else{
          $pagination .= "<span class=\"disabled\">next »</span>";
          $pagination .= "</div>\n";       
        }
       }
      echo $pagination;
    }

    ?>

 

only happens when i've typed more than one word in the search bar.

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.