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
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
Share on other sites

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.