tlflow Posted March 28, 2007 Share Posted March 28, 2007 I'm trying to do a search in which I am trying to get results back if client user types in either: both first name, last name and phone number just phone number (either phone1 or phone2) just first name just last name I know I can do this just by programming a bunch of if statements in my code...but is there a way to do this just by using MySQL? SELECT * FROM people WHERE ((user_firstname = 'jake' OR user_lastname = '') OR (phone1 = '7041112525' OR phone2 = '')) Thanks for the help, tlflow Quote Link to comment Share on other sites More sharing options...
tlflow Posted April 3, 2007 Author Share Posted April 3, 2007 Anyone that could help me with this one? Thanks, TLFLOW Quote Link to comment Share on other sites More sharing options...
Wildbug Posted April 3, 2007 Share Posted April 3, 2007 While building the query in PHP would be simpler, you could try something like this: SELECT * FROM people WHERE ('' != '' AND user_firstname='') OR ('' != '' AND user_lastname='') OR ('' != '' AND phone1='') OR ('' != '' AND phone2='') Except that the first and last pairs of single quotes will contain your possible search string. I don't know how you intend to populate the query, but maybe like this: SELECT * FROM people WHERE ('$fn' != '' AND user_firstname='$fn') OR ('$ln' != '' AND user_lastname='$ln') OR ('$ph1' != '' AND phone1='$ph1') OR ('$ph2' != '' AND phone2='$ph2') That way if the string is empty, the rest of the parenthetical subcondition is ignored. (That took alot of thought.) :-) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.