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
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:
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";
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?
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,