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? 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%' 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! Link to comment https://forums.phpfreaks.com/topic/237254-simplifying-this-sql/#findComment-1219222 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.