SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member fuhsheeez's Avatar
    Join Date
    Oct 2004
    Location
    Boston, MA
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Searching a MySQL database

    Ok so I have completed searching the MySQL part as I found a script online that seems nice and works well.

    http://www.campusbreak.com/content/search/search.php

    To try it search 'Amazing'

    One query comes up but I can't drag out the variable $filename from the database. I tried to mix another query into the script but it only would give me errors on non-existant lines.

    Below I have the entire search2.php code. If anyone can help me that would be great, I can send some traffic *wink* *wink*.

    PHP 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

    // 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","*******","****"); //(host, username, password)

    //specify database ** EDIT REQUIRED HERE **
    mysql_select_db("******") or die("Unable to select database"); //select which database we're using

    // Build SQL Query  
    $query "select * from Amazing where description like \"%$trimmed%\" order by description"// 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=\"_google\" 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("Couldnt 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 $s ;

    // now you can display the results returned
      
    while ($rowmysql_fetch_array($result)) {
      
    $title $row["description"];


      echo 
    "$count.)&nbsp;<a href=http://www.campusbreak.com/index.php?x=moviewatch&media=$filename>$title</a>" ;
      
    $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 ;
      echo 
    "<p>Showing results $b to $a of $numrows</p>";
      
    ?>

  2. #2
    Non-Member
    Join Date
    Jul 2005
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't quite understand the problem, are you saying you want to pull out another column from the database?

    If that's all then you just need to define it in a variable and use it wherever you need it in the loop:

    PHP Code:
    $filename $row["filename"];

    echo 
    $filename

  3. #3
    SitePoint Member fuhsheeez's Avatar
    Join Date
    Oct 2004
    Location
    Boston, MA
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Bob Carologees
    I don't quite understand the problem, are you saying you want to pull out another column from the database?

    If that's all then you just need to define it in a variable and use it wherever you need it in the loop:

    PHP Code:
    $filename $row["filename"];

    echo 
    $filename
    Yes but I want is associated with the other column being grabbed.

    So how would I grab according to $description

    Wouldnt you setup a query ie



    PHP Code:
    $$query='SELECT * ' ' FROM `Amazing` ' ' WHERE `$desciption = description`'
    [/QUOTE]

    Or something like that?

  4. #4
    Non-Member
    Join Date
    Jul 2005
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $query "SELECT * from Amazing WHERE description = 'Amazing.description' order by description"
    You mean this? I still don't totally see the problem.

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2003
    Location
    Canada
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm a little confused. Where in the database is the column filename in relation to the other columns? Same table? Different table (name it please)?

    Also, you should be using mysql_escape_string() on your $_GET var to avoid injection attacks.


Bookmarks

Posting Permissions

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