SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    webality...dunno what it means bcp_2005's Avatar
    Join Date
    Jun 2002
    Location
    Huntsville Alabama
    Posts
    394
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with very simple search script...

    Oke here's my problem. I've got this databse of sentances. I need to search this database of sentances and return any results. Such as, if you search for the word 'php' it would return a value of all the sentances that had that word in it. Here's the script I came up with:

    $query = "SELECT name FROM search";

    $result = mysql_query($query);

    if (preg_match ("/$term/i", "$result")) {
    print "A match was found.";
    } else {
    print "A match was not found.";
    }

    I've got this test sentance in the db:

    PHP rules.

    I search for 'PHP'. It says "a match was not found". What am I doing wrong?
    [ metabahn ] : changing the course of the web.

  2. #2
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    Code:
    $query = mysql_query("select name from search where name like '%$term%'");
    $num = mysql_num_rows($result);
    if ($num > 0) {
        echo "<p>Found $num results</p>\n";
    } else {
        echo "<p>No results found</p>\n";
    }
    while ($row = mysql_fetch_array($query)) {
        echo "Result: {$row['name']}<br />\n";
    }

  3. #3
    webality...dunno what it means bcp_2005's Avatar
    Join Date
    Jun 2002
    Location
    Huntsville Alabama
    Posts
    394
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works! I do have a question though, in the mysql query you have this:

    $query = mysql_query("select name from search where name like '%$term%'");

    what do the '%' do? I've seen that before but have no clue what purpose they serve...

    anyways, thanks for your time!
    [ metabahn ] : changing the course of the web.

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2000
    Location
    Catalonia
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The % are used as wildcards. They work the same way as * in Windows.
    ie: ...where name like '%$term%'...
    if there's a name "dkjaoTERMadka" it will be selected by the query.

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2002
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    '$term%' would match a first word
    '%$term' would match a last word

  6. #6
    webality...dunno what it means bcp_2005's Avatar
    Join Date
    Jun 2002
    Location
    Huntsville Alabama
    Posts
    394
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok i get it thanks
    [ metabahn ] : changing the course of the web.


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
  •