Issue with the LIKE expression and php

Reply

Join Date: May 2005
Posts: 1
Reputation: tristan is an unknown quantity at this point 
Solved Threads: 0
tristan tristan is offline Offline
Newbie Poster

Issue with the LIKE expression and php

 
0
  #1
May 15th, 2005
Hello,

I am having a members search page, where you can look up members by searching on e.g. age, interests, country etc. If the user does not specify e.g. an age range, then all ages should be included in the search. But if a specific age range is specified, then only this age range should be included.

I have tried solving it with a LIKE expression, where I use the wildcard "%" if nothing is specified. This sometimes works correctly, but other times it produces a wrong output by ignoring some of the specifications. I cannot figure out as to why this happens, but guess that it has something to do with variables being kept when I try a new search, so that the variables become messed together.

Some sample code:


[PHP]if (!$pet || @$pet == "" || (@$pet != "Cat" && @$pet != "Dog" && @$pet != "Horse" && @$pet != "Lion" && @$pet != "Whale" && @$pet != "Dolphin" && @$pet != "Elephant" && @$pet != "Mouse")) $pet = "%";[/PHP]

And thus the query goes:

[PHP]$query = "SELECT pet FROM table WHERE (pet LIKE '$pet')";[/PHP]

The "funny" thing is that it works for choosing gender (man or woman):

[PHP]if (!$mw || @$mw == "" || (@$mw != "Woman" && @$mw !="Man")) $mw = "%";[/PHP]

And the query:

[PHP]$query = "SELECT mw from table WHERE (mw LIKE '$mw')";[/PHP]

To clarify things, I have only one query for the whole search. The man/woman specification works every time, but not the longer one with more options to choose from.

  1. SELECT mw,pet FROM table WHERE (mw LIKE '$mw') AND (pet LIKE '$pet') AND ...

Does anyone have a better solution for enabling searches where you can leave certain fields blank in order to obtain any value, and at the same time return rows where a specified value has been provided by the user?

Thank you very much in advance. I have spent a whole day just with this issue.

Nico
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 138
Reputation: sarahk is an unknown quantity at this point 
Solved Threads: 1
sarahk's Avatar
sarahk sarahk is offline Offline
Junior Poster

Re: Issue with the LIKE expression and php

 
0
  #2
May 16th, 2005
Build the query up by appending to the string.

  1. //assume all variables are safe and have addslashes()
  2. $query = "select * from `table` ";
  3. $joiner = ' where ';
  4. if (!empty($mw))
  5. {
  6. $query .= $joiner . "`mw` = '{$mw}' ";
  7. $joiner ' and ';
  8. }
  9. if (!empty($pet))
  10. {
  11. $query .= $joiner . "`pet` = '{$pet}' ";
  12. $joiner = ' and ';
  13. }
and so on until the query is complete

I don't think you necessarily need to use like but if you do then have % to mark where the like is used.

Sarah
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC