Results 1 to 10 of 10

Thread: Searching Database for Item

  1. #1
    Join Date
    Apr 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Searching Database for Item

    Hi, I'm a total newbie at this so any help will be greatly appreciated...
    Ok, heres what I wanna do:
    I want to place an Input textbox on my Index with a submit button to search for details In my Database and display them In a webpage...
    DB table name: nk_table
    table fields: id, firstname, lastname, address, phone, mobile, email
    I want to display full details whenever I search for something...
    E.g: when I search for firstname I want the other Info (lastname, address, phone...) to display as well...

    So, Is there such a code or script?

    Thanx In Advance.

  2. #2
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Please provide a more descriptive title, Things along the lines of "help/script needed" or "problem" are to vauge.
    Mysql:
    SELECT * FROM nk_table WHERE MATCH (*) AGAINST (keyword);

    That searches the entire table for the keyword...
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  3. #3
    Join Date
    Apr 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    sorry about the title, 'n thanx for ur reply ...
    But, how do I use that code!!

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

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

    // Build SQL Query
    $query = "SELECT * FROM nk_table WHERE firstname LIKE \"&#37;$trimmed%\";
    order by firstname"; // 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["1"];

    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>";

    ?>
    Last edited by unicorn; 04-17-2007 at 05:35 PM.

  4. #4
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    USE {PHP} BB TAGS.
    Code:
    <?php
    ...
    //connect to your database ** EDIT REQUIRED HERE **
    mysql_connect("hidden","hidden","hidden"); //(host, username, password)
    
    //specify database ** EDIT REQUIRED HERE **
    mysql_select_db("hidden") or die("Unable to select database"); //select which database we're using
    
    // Build SQL Query
    $query = "SELECT MATCH (*) AGAINST (\"&#37;$trimmed%\") AS score FROM nk_table WHERE MATCH (title,body) AGAINST (\"%$trimmed%\");
    order by score"; // 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
    ...  
    ?>
    Changed the query to display by relevancy, may or may not work.
    For future reference:
    http://dev.mysql.com/doc/refman/5.0/...xt-search.html
    Last edited by boxxertrumps; 04-17-2007 at 09:08 PM.
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  5. #5
    Join Date
    Apr 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    Doesn't work ... I keep gettin'

    Results
    Sorry, your search: "keyword" returned zero results

    Click here to try the search on google

    Couldn't execute query

  6. #6
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    for some reason the line with this:
    $numrows=mysql_num_rows($numresults);
    Was commented.
    Fixed the code.
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  7. #7
    Join Date
    Apr 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ahh, one more thing ...
    shall I leave the (*) and (title,body) like that? Or shall I replace them with the fields name?

    Sorry 4 the disturbance but as I said I'm a total newbie at this.

  8. #8
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Yes, on this line you need to edit the parts in red:

    Code:
    $query = "SELECT MATCH (*) AGAINST (\"&#37;$trimmed%\") AS score FROM nk_table WHERE MATCH (title,body) AGAINST (\"%$trimmed%\");
    order by score";
    to match your database table structure.
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  9. #9
    Join Date
    Apr 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Looks like It isn't meant for this to work
    everytime I get a different stupid error, like:

    Parse error: parse error, unexpected $ in /var/www/html/search.php on line 102
    The funny thing is that I only have "?>" << this code on line 102 which is the end of the script

  10. #10
    Join Date
    Mar 2007
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    look at the line above that and see if you left out a ;

    could be the problem

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
  •