webguync Posted May 23, 2011 Share Posted May 23, 2011 I have some SQL fpr a search form that I am working on which searches across a MySQL table. There are quite a few fields I want searchable and I currently have the code as such $term = strip_tags(substr($_POST['search_term'],0, 100)); $term = mysql_escape_string($term); $sql = "select * from Phase1A_1B_TotalScores_2011 where last_name like '%$term%' or first_name like '%$term%' or employee_id like '%$term%' or title like '%$term%' or territory like '%$term%' or district like '%$term%' or Phase1A_Score like '%$term%' or Phase1B_HS_Exam like '%$term%' or Phase1A_HS_Exam_RT like '%$term%' or Phase1B_HS_Exam like '%$term%' or Phase1B_HS_Exam_RT like '%$term%' or Class_Date like '%$term%' I understand it is not a good idea to use a bunch of OR clauses. Is there a way to simplify this? Quote Link to comment https://forums.phpfreaks.com/topic/237254-simplifying-this-sql/ Share on other sites More sharing options...
AbraCadaver Posted May 23, 2011 Share Posted May 23, 2011 I'm not sure which is faster or best practice, but you could do: $sql = "SELECT * FROM Phase1A_1B_TotalScores_2011 WHERE CONCAT(last_name,first_name,employee_id,title,territory,district,Phase1A_Score,Phase1B_HS_Exam, Phase1A_HS_Exam_RT,Phase1B_HS_Exam ,Phase1B_HS_Exam_RT,Class_Date) LIKE '%$term%' Quote Link to comment https://forums.phpfreaks.com/topic/237254-simplifying-this-sql/#findComment-1219215 Share on other sites More sharing options...
webguync Posted May 23, 2011 Author Share Posted May 23, 2011 I think I like that better, much cleaner. Thx! Quote Link to comment https://forums.phpfreaks.com/topic/237254-simplifying-this-sql/#findComment-1219222 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.