www.webdeveloper.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 36

Thread: search string in mysql

  1. #1
    Join Date
    Jan 2004
    Posts
    312

    search string in mysql

    Can someone provide me a sample code how I can have a "search" field search for any keyword that matches my records in my database.

    I have a list of agencies we fund, they have a 25 word description column. If someone searches for any word and it matches that of the "description" column list records by ID desc.

    Is this possible to do a simple search query? find word and/or and list records, otherwise return sorry, no records showing that keyword.

    Thanks

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,616
    PHP Code:
    $search $_POST['search_string'];
    $query "SELECT * FROM `table_name` WHERE `description` LIKE '%$search%'";
    $result mysql_query($query) or die(mysql_error());
    while(
    $row mysql_fetch_assoc($result))
    {
      
    # echo columns of interest in desired sequence and format

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2004
    Posts
    312
    How do I add "name" into there, either name or description columns, they might search the description of the organization or the name of itself.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,616
    PHP Code:
    $query "SELECT * FROM `table_name` WHERE `description` LIKE '%$search%' OR `name` LIKE '%$search%'"
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jan 2004
    Posts
    312
    Thanks, I will try that.

  6. #6
    Join Date
    Jan 2004
    Posts
    312
    Okay, it works but with some search problems. Is there a way to make it NOT case-sensitive. For example, I will search for family, and records appear, but if I'm searching for an agency called Family it won't find unless I make the "f" capitals. is there a way to force it. Also, if I put "spaces" in between the words it won't find any at all. Is there also a way to say, if there is a space in between the words, take either or both words and try to find a match. First try to find a match for the full search, if nothing then search for each word separately without being case sensitive.

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,616
    Not tested, so no guarantees:
    PHP Code:
    # convert to upper case, trim it, and replace spaces with "|":
    $search strtoupper(preg_replace('/\s+/''|'trim($_POST['search_string'])));
    # create a MySQL REGEXP for the search:
    $regexp "REGEXP '^.*($search).*\$'";
    $query "SELECT * FROM `table_name` WHERE UPPER(`description`) $regexp " .
             
    "OR UPPER(`name`) $regexp";
    $result mysql_query($query) or die(mysql_error());
    while(
    $row mysql_fetch_assoc($result))
    {
      
    # echo columns of interest in desired sequence and format

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Jan 2004
    Posts
    312
    That workes fine but I realized on error. Say I put red cross, it will find everything word that has red within it, not a specific word. Its finding anything that has red in it, for example: Redding, Freddy...is there a way to say only ifs a "word" not the letters within it.

  9. #9
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,616
    Give this a shot (said with a tone of uncertainty):
    PHP Code:
    $regexp "REGEXP '^.*[[:<:]]($search)[[:>:]].*\$'"
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  10. #10
    Join Date
    Jan 2004
    Posts
    312
    It doesn't error out but it doesn't find any more data, it just won't return results.

  11. #11
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,616
    Maybe...
    PHP Code:
     $regexp "REGEXP '[[:<:]]($search)[[:>:]]'"
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  12. #12
    Join Date
    Jan 2004
    Posts
    312
    No..same...thanks so much for trying to help NogDog, you have know idea how much I appreciate getting this far!!!

  13. #13
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,616
    Tried this test on a DB on my PC, seemed to work pretty well. Just posting here for you to see what I'm trying in a complete context.
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
    "http://www.w3.org/TR/html4/strict.dtd">
    <html lang='en'>
    <head>
    <META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>
    <title>Page title</title>
    <style type="text/css">
    table {
      border-collapse: collapse;
      border: solid 1px black;
    }
    td {
      padding: 2px 6px;
      border: solid 1px black;
    }
    </style>
    </head>
    <body>
    <form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
    <p>Search for: <input type='text' name='search' size='20' maxlength='64'></p>
    <p><input type='submit' value='Search'></p>
    </form>
    <?php
    if(isset($_POST['search']))
    {
      
    $connx mysql_connect('localhost''*****''*****') or die("connx");
      
    $db mysql_select_db('test') or die(mysql_error());
      
    # convert to upper case, trim it, and replace spaces with "|":
      
    $search = (ini_get('magic_quotes_gpc')) ? stripslashes($_POST['search']) :
                
    $_POST['search'];
      
    $search mysql_real_escape_string($search);
      
    $search strtoupper(preg_replace('/\s+/''|'trim($_POST['search'])));
      
    # create a MySQL REGEXP for the search:
      
    $regexp "REGEXP '[[:<:]]($search)[[:>:]]'";
      
    $query "SELECT * FROM `users` WHERE UPPER(`description`) $regexp OR ".
               
    "`name` $regexp";
      
    $result mysql_query($query) or die($query " - " mysql_error());
      echo 
    "<table>\n";
      while(
    $row mysql_fetch_assoc($result))
      {
        echo 
    "<tr>";
        foreach(
    $row as $key => $value)
        {
          echo 
    "<td>$value</td>";
        }
        echo 
    "</tr>\n";
      }
    }
    ?>
    </body>
    </html>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  14. #14
    Join Date
    Jan 2004
    Posts
    312
    That did it, thanks so much!

  15. #15
    Join Date
    May 2006
    Posts
    138
    The above code works great, is it possible to allow a part of the search string with one column only. The way the code is setup it looks for "whole" words. I have 1 column thats 5 digits for customer codes, but some are 4 digits. The 4 digits would normally have a 0 infront like this 01234 but its 1234, so If I search for 1234 it finds the record, if I search for 01234 it doesn't find the record. Can I tell it on column 'bn2' to allow a fragment of the code to work?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles