Can anyone find the mistake in these script please...?

Discussion in 'PHP' started by hasansahip, Mar 18, 2008.

  1. #1
    i am a newbie at php.
    Hi i have a script got it from a website but something is wrong with this script. it is a search engine connected to my database. the problems are:
    1)It does not display one by one (pagination does not work) .It shows the results on one row.
    2)when i do not type anything in the text box and click on the submit button, it does display the last searched keyword.

    this is my script
    <?php
      // rows to return
      $limit = 10; 
      // Get the search variable from URL
      $var = @$_GET['q'] ;
      $trimmed = trim($var);
    
    
    mysql_connect("localhost", "root", "");
    mysql_select_db("deneme1") or die("Unable to select database"); 
    
    // Build SQL Query  
    $query = "select * from members where username like \"%$trimmed%\" 
      order by username"; 
    
     $numresults=mysql_query($query);
     $numrows=mysql_num_rows($numresults);
    
    if ($numrows == 0)
      {
     	 echo "<h4>Results</h4>";
         echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";
      }
    
    // next determine if s has been passed to script, if not use 0
      if (empty($s)) {
      $s=0;
      }
    
    // get results
      $query .= " limit $s,$limit";
      $result = mysql_query($query) or die("Couldn't execute query");
    
    // display what the person searched for
    echo "<p>You searched for: &quot;" . $var . "&quot;</p>";
    
    // begin to show results set
    echo "Results";
    $count = 1 + $s ;
    
    // now you can display the results returned
      while ($row= mysql_fetch_array($result)) {
      $title = $row["username"];
    
      echo "$count.)&nbsp;$title" ;
      $count++ ;
      }
    
    $currPage = (($s/$limit) + 1);
    
    //break before paging
      echo "<br />";
    
      // next we need to do the links to other results
      if ($s>=1) { // bypass PREV link if s is 0
      $prevs=($s-$limit);
      print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt; 
      Prev 10</a>&nbsp&nbsp;";
      }
    
    // calculate number of pages needing links
      $pages=intval($numrows/$limit);
    
    // $pages now contains int of pages needed unless there is a remainder from division
    
      if ($numrows%$limit) {
      // has remainder so add one page
      $pages++;
      }
    
    // check to see if last page
      if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
    
      // not last page so give NEXT link
      $news=$s+$limit;
    
      echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 &gt;&gt;</a>";
      }
    
    $a = $s + ($limit) ;
      if ($a > $numrows) { $a = $numrows ; }
      $b = $s + 1 ;
      echo "<p>Showing results $b to $a of $numrows</p>";
      
    ?>
    PHP:
    can anyone help me please?
    thanks in advance.
     
    hasansahip, Mar 18, 2008 IP
  2. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It would help if you could post the URL where this is working so we can see it in action.
     
    Christian Little, Mar 18, 2008 IP
  3. hasansahip

    hasansahip Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i do not have the url (because my friend gave me this code and he does not understand too)but i can show you the html code:
    <html>
    <head>
    <title>Search </title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    
    <form name="form" action="search12.php" method="get">
    
      Search : <input type="text" name="q" />
      <input type="submit" name="Submit" value="Search" />
      
    </form>
    </body>
    
    PHP:
    i use wamp server on windows xp. Thanks.
     
    hasansahip, Mar 18, 2008 IP
  4. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    <?php
    
      $q = $_GET['q'];
    
      mysql_connect("localhost", "root", "");
      mysql_select_db("deneme1") or die("Unable to select database");
    
      // Build SQL Query 
      $query = "select * from members where username like \"%$q%\"
      order by username";
    
      $result = mysql_query($query);
    
      $counter = 0;
    
      while($row = mysql_fetch_object($result)) {
        $counter++;
        echo "$counter) $row->username<BR>";
      }
    
      if(!$counter) {
        echo "<p>Sorry, your search for \"$q\" returned zero results.</p>";
      }
    
    ?>
    
    PHP:
    This won't fix your problem, but I can barely read the code you posted. Try this code, it's a way more efficient way of doing what you were doing. Then add the other features once this basic version works.
     
    Christian Little, Mar 19, 2008 IP
  5. hasansahip

    hasansahip Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    this is the same even when i click on the submit button it shows all the name from the database
     
    hasansahip, Mar 19, 2008 IP