Search PHP script

Discussion in 'PHP' started by PET, Mar 17, 2007.

  1. #1
    Hello,

    I want to do a Search PHP script that will search something in the database.
    Check the screenshot. That's how the form is looking. Nothing more. I just put the text, and select WHAT I want to search.
    Also, what I search is the table name, so the query is tomething like this:

    $query = "SELECT * FROM ". $drop_down ." ................

    The problem is that I want to search anything, in any column from that table. For exemple I imput:

    Bush

    I want the query to search each column for the text Bush. And display it if he finds anything in any column.

    Any sugestions?


    Later edit:
    if I do somethink like this:
    
    SELECT * FROM ". $search_type ." where `nume` like '%". $search_what ."%'";
    
    PHP:
    It works, the thing is that It only searches in `nume` not in all the fields :)
     

    Attached Files:

    PET, Mar 17, 2007 IP
  2. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Because you said "where `nume` like" .. just add "OR" for each additional field:

    
    SELECT * FROM ". $search_type ." where `nume` like '%".$search_what."%' OR `another_field` like '%".$search_what."%'";
    
    PHP:
     
    CodyRo, Mar 17, 2007 IP
  3. Alam

    Alam Active Member

    Messages:
    316
    Likes Received:
    91
    Best Answers:
    0
    Trophy Points:
    68
    #3
    Try like this


    $search_keywords=strtolower(trim($q));
    $arrwords=explode(" ",$search_keywords);
    $arrwords=array_unique($arrwords);
    $searchwords=array();
    $junkwords=array();
    foreach($arrwords as $word)
    if(!$commonwords[$word]){
    $searchwords[]=$word;
    }
    else
    {
    $junkwords[]=$word;
    }

    $temp_str="links_keywords like '%";
    $keyword_str=implode("%' or $temp_str",$searchwords);
    $keyword_str_keywords=$temp_str.$keyword_str."%'";


    $temp_str="links_title like '%";
    $keyword_str=implode("%' or $temp_str",$searchwords);
    $keyword_str_title=$temp_str.$keyword_str."%'";

    $temp_str="links_content like '%";
    $keyword_str=implode("%' or $temp_str",$searchwords);
    $keyword_str_content=$temp_str.$keyword_str."%'";

    $keyword_str="$keyword_str_keywords and $keyword_str_title and $keyword_str_content";


    $sql="select * from links_links where status=1 and $keyword_str order by links_hits desc limit $offset,$limit";
     
    Alam, Mar 18, 2007 IP
  4. PET

    PET Member

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #4
    The thing is that I have about 30 fields, just think how long will be the query... with OR... and OR...and OR. About the Alam sugestion, I will try it. Just a question, how should I display my results?
     
    PET, Mar 18, 2007 IP
  5. ketan9

    ketan9 Active Member

    Messages:
    548
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #5
    This is not efficient way to do a search. It might be OK for small records but if your db grows then probably it would take for ever with high traffic!! So think on it. If your foundation is solid then only your website will be able to support the growth otherwise it will fumble. Do not go easy way and cook up spagetti code. Plan it proper thinking that in future will it be fast with high traffic!!

    My 2 cents,
     
    ketan9, Mar 20, 2007 IP