Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    PHP Search script problem

    Can anyone please tell me what is the problem on this script getting error on line 20.

    Code:
    <html>
    <head>
    <title>search script</title>
    </head>
    <body>
    
    <form name="form" action="search.php" method="get">
      <input type="text" name="q" />
      <input type="submit" name="Submit" value="Search" />
    </form>
    
    <?php
    
      // Get the search variable from URL
    
      $var = @$_GET['q'] ;
      $trimmed = trim($var) //trim whitespace from the stored variable
    
    // rows to return
    $ limit = 10; 
    
    // check for an empty string and display a message.
    if ($trimmed == "")
      {
      echo "<p>Please enter a search...</p>";
      exit;
      }
    
    // check for a search parameter
    if (!isset($var))
      {
      echo "<p>We dont seem to have a search parameter!</p>";
      exit;
      }
    
    //connect to your database ** EDIT REQUIRED HERE **
    mysql_connect("localhost","root",""); //(host, username, password)
    
    //specify database ** EDIT REQUIRED HERE **
    mysql_select_db("search") or die("Unable to select database"); //select which database we're using
    
    // Build SQL Query  
    $query = "select * from search where name like \"%$trimmed%\"  
      order by name"; // EDIT HERE and specify your table and field names for the SQL query
    
     $numresults=mysql_query($query);
     $numrows=mysql_num_rows($numresults);
    
    // If we have no results, offer a google search as an alternative
    
    if ($numrows == 0)
      {
      echo "<h4>Results</h4>";
      echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";
    
    // google
     echo "<p><a href=\"http://www.google.com/search?q=" 
      . $trimmed . "\" target=\"_blank\" title=\"Look up 
      " . $trimmed . " on Google\">Click here</a> to try the 
      search on google</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["1st_field"];
    
      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>";
      
    ?>
    
    </body>
    </html>

  • #2
    New Coder
    Join Date
    Mar 2009
    Posts
    90
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Code:
    <html>
    <head>
    <title>search script</title>
    </head>
    <body>
    
    <form name="form" action="search.php" method="get">
      <input type="text" name="q" />
      <input type="submit" name="Submit" value="Search" />
    </form>
    
    <?php
    
      // Get the search variable from URL
    
      $var = @$_GET['q'] ;
      $trimmed = trim($var); //trim whitespace from the stored variable
    
    // rows to return
    $limit = 10; // no spaces here
    ................................
    You've also to pass $trimmed in mysql_real_escape_string() before quering DB.
    Regards

  • Users who have thanked koko5 for this post:

    yasirjamal (03-07-2010)

  • #3
    New Coder
    Join Date
    Feb 2009
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank You KOKO

    Its working now. But the problem is now. What i want the search result appear in a nice way. Like Name fisrt then discription after that URL. Can you please tell me how i do that. Because know its only showing 1 result found and keyword.

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    90
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Hi yasirjamal,

    You've to change:
    PHP Code:
    // now you can display the results returned
      
    while ($rowmysql_fetch_array($result)) {
      
    $title $row["1st_field"];

      echo 
    "$count.)&nbsp;$title" ;
      
    $count++ ;
      } 
    to
    PHP Code:
    while ($rowmysql_fetch_array($result)) {
      
    $title $row["1st_field"];

      echo 
    "{$count}.)&nbsp;Name:{$row['DBtable_field_name']}&nbsp;{$title}" ;
      
    $count++ ;
      } 
    where "DBtable_field_name" should be renamed to your DB table's column name which holds first name (or you can echo $row[0] ... $row[1] to display any other desired columns. I mean $row can be accessed as associative or non-associative array).

    Analogic for any other data you wish to display.
    Regards
    Edit:I saw now in your code:
    // Build SQL Query
    $query = "select * from search where name like \"%$trimmed%\"
    so field name is "name". Than display should be:
    PHP Code:
    while ($rowmysql_fetch_array($result)) {
      
    $title $row["1st_field"];

      echo 
    "{$count}.)&nbsp;Name:{$row['name']}&nbsp;{$title}" ;
      
    $count++ ;
      } 
    Last edited by koko5; 03-07-2010 at 12:01 PM.

  • Users who have thanked koko5 for this post:

    yasirjamal (03-07-2010)

  • #5
    New Coder
    Join Date
    Feb 2009
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks alot koko,

    Its working fine now. I have few more question and i know u are a professional programmer can solve it very quickly.

    1: If you write some wrong keyword on website it will show you sorry your search "keyword" result zero. Its fine i also want if someone type wrong thing it will tell people. But it will not stop there it will also show you the that you search for " keyword" at the bottom site it will show you next link which i dont want if you check my script you will understand better.

    2: Pagination link is not working when i click next button it will show the same page again and also i want previous links as well.

    3: How i can use css style on php script.

    4: Do you think this script is fine to use it on website?

    If you can answer me these question it will be a greate help!!

    Code:
    <?php
    
      // Get the search variable from URL
    
      $var = @$_GET['q'] ;
      $trimmed = trim($var); //trim whitespace from the stored variable
    
    // rows to return
    $limit = 10; // no spaces here
    
    // check for an empty string and display a message.
    if ($trimmed == "")
      {
      echo "<p>Please enter a search...</p>";
      exit;
      }
    
    // check for a search parameter
    if (!isset($var))
      {
      echo "<p>We dont seem to have a search parameter!</p>";
      exit;
      }
    
    //connect to your database ** EDIT REQUIRED HERE **
    mysql_connect("localhost","root",""); //(host, username, password)
    
    //specify database ** EDIT REQUIRED HERE **
    mysql_select_db("search") or die("Unable to select database"); //select which database we're using
    
    // Build SQL Query  
    $query = "select * from search where name like \"%$trimmed%\"  
      order by name"; // EDIT HERE and specify your table and field names for the SQL query
    
     $numresults=mysql_query($query);
     $numrows=mysql_num_rows($numresults);
    
    // If we have no results, offer a google search as an alternative
    
    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 "<b>You searched for: &quot;" . $var . "&quot;</b><br><hr>";
    
    // 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["name"];
      $dis = $row["discription"];
      $url = $row["url"];
    
      echo "
      <b>$title</b><br>
      $dis<br>
      <a href='$url'>$url</a><br><br>
      ";
      
      $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=\"search.php?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=\"search.php?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>";
      
    ?>
    Last edited by yasirjamal; 03-07-2010 at 01:35 PM.

  • #6
    New Coder
    Join Date
    Mar 2009
    Posts
    90
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Hi yasirjamal, sorry for the delay,

    Of course you can use css, simply include('yourfile.css'); in header section of your page. I think this code can be used, but please check also professional solutions , provided and tested by master coders:

    You can find great information about pagination links generation and securing your script from sql injections.
    Finding answers yourself (to teach fishing) is the best option.
    Thanks
    p.p.: searching this forum you can find ready-to-use solutions, which have more than desired functionality and post questions to the authors.
    I didn't post all interesting links.
    Last edited by koko5; 03-07-2010 at 04:19 PM.

  • #7
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Quote Originally Posted by yasirjamal View Post
    4: Do you think this script is fine to use it on website?
    I'll answer #4. If you want your site hacked then yes use this script on a website. If don't want it hacked then I suggest you look into mysql injection to learn what it is and how to prevent it.

    http://www.tizag.com/mysqlTutorial/m...-injection.php
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #8
    New Coder
    Join Date
    Feb 2009
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank koko, Aerospace_Eng.

    I am new to php can you please refer me a good search engine script to search my database. A secure script with pagination option

    Thanks alot

  • #9
    New Coder
    Join Date
    Feb 2009
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts
    how to delete my posts
    Last edited by yasirjamal; 03-08-2010 at 07:42 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •