I am using following search query for searching records from table.
I have 5 to 6 columns and I want to add columns only if value is not empty.
SELECT * FROM table WHERE
(
(description like '$Keyword' OR name like '$Keyword' OR username like '$Keyword') OR
(qualification like '$Qualification') OR
(nationality like '$Nationality') OR
(country like '$Country') OR
(city like '$City')
)
AND active='1'
ORDER BY tid
I want to add qualification ,nationality and other columns in query only if they are not empty.
Therefore I created following conditional php code but it is adding extra OR or missing OR sometimes.
if($Status!="") { $active = "AND active='$Status' "; }
if($Keyword!="") { $where .= " (description like '$Keyword' OR name like '$Keyword' OR username like '$Keyword') OR "; }
if($Qualification!="") { $where .= " (qualification like '$Qualification') OR "; }
if($Nationality!="") { $where .= " (nationality like '$Nationality') OR "; }
if($Country!="") { $where .= " (country like '$Country') OR "; }
if($City!="") { $where .= " (city like '$City') "; }
What will be the best way to do this using SQL or PHP.
One thing to point out, do you really need all the fields from the table in the result set? If you don’t you should specify just the ones you need in the SELECT clause