Page 1 of 1

Using AND and OR statements in a MYSQL Query

Posted: Mon Aug 18, 2008 12:41 pm
by mrgooding
Hi All

I'm looking to create a basic search function - someone can put in a phone number, and it will come up with any contact(s) that have the phone number assigned. This works, however, I (for obvious reasons) only want users to be able to search their own contacts, not that of others.

Code: Select all

SELECT * FROM contacts WHERE c_tel_home='$keyword' OR c_tel_work='$keyword' OR c_mob_home='$keyword' OR c_mob_work='$keyword' AND u_id='$Userid'
when I run the above query, it doesn't take into account the AND u_id bit, and therefore searches everyones contacts, not just the given user.

This doesn't happen if I only use:

Code: Select all

SELECT * FROM contacts WHERE c_tel_home='$keyword' AND u_id='$Userid'
i.e. this filters out all contacts, except for the current user.

Is there a way to make my first select statement work like this?

Any pointers would be appreciated.

Re: Using AND and OR statements in a MYSQL Query

Posted: Mon Aug 18, 2008 12:59 pm
by mrgooding
Solved it - missing brackets!