Ahmad Sajid
Ahmad Sajid

Reputation: 133

MySql search name and email in multiple column

I am facing a problem matching/ searching values in multiple column in mysql.

In my table i have name, owner, email, altemail, altemail2, altemail3 column name.

name = John; email = [email protected]

now name might be in name or owner column and email might be in above four column.

now i want to make a search and list the result where both name and email is present. means name and email should be matched not just name or email.

this is what i have done so far but not working :

SELECT*                     
FROM $table
WHERE name = '%s' OR owner = '%s' AND email = '%s' OR alt_email = '%s'
OR alt_email2 = '%s' OR alt_email3 = '%s'",$name,$email,$email,$email,$email);

but here if name matched it shows the result and disregards the email or if email matched it shows result disregards the name. but i need to that user info to match name and email both.

Thanks again for help.

Upvotes: 0

Views: 617

Answers (2)

eLemEnt
eLemEnt

Reputation: 1801

change your query to this

SELECT*                     
FROM $table
WHERE (name = '%s' OR owner = '%s') AND (email = '%s' OR alt_email = '%s'
OR alt_email2 = '%s' OR alt_email3 = '%s')",$name,$name,$email,$email,$email,$email);

Upvotes: 1

Pathik Vejani
Pathik Vejani

Reputation: 4501

This should work:

SELECT * FROM $table WHERE (name = '%s' OR owner = '%s') AND (email = '%s' OR alt_email = '%s' OR alt_email2 = '%s' OR alt_email3 = '%s')",$name,$email,$email,$email,$email);

Upvotes: 0

Related Questions