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
    22,326
    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

    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  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
    22,326
    PHP Code:
    $query "SELECT * FROM `table_name` WHERE `description` LIKE '%$search%' OR `name` LIKE '%$search%'"
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  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
    22,326
    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

    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  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
    22,326
    Give this a shot (said with a tone of uncertainty):
    PHP Code:
    $regexp "REGEXP '^.*[[:<:]]($search)[[:>:]].*\$'"
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  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
    22,326
    Maybe...
    PHP Code:
     $regexp "REGEXP '[[:<:]]($search)[[:>:]]'"
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  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
    22,326
    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>
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.20462 seconds
  • Memory Usage 3,032KB
  • Queries Executed 15 (?)
More Information
Template Usage (36):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (6)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (15)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (15)postbit
  • (15)postbit_onlinestatus
  • (15)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (74):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates