Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.