Jump to content

simplifying this SQL


webguync

Recommended Posts

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

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%'

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.