Jump to content

Search Database Query


tlflow

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/44635-search-database-query/
Share on other sites

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.)  :-)

Link to comment
https://forums.phpfreaks.com/topic/44635-search-database-query/#findComment-220734
Share on other sites

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.