Using AND and OR statements in a MYSQL Query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
mrgooding
Forum Newbie
Posts: 23
Joined: Thu May 22, 2008 11:45 am

Using AND and OR statements in a MYSQL Query

Post 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.
mrgooding
Forum Newbie
Posts: 23
Joined: Thu May 22, 2008 11:45 am

Re: Using AND and OR statements in a MYSQL Query

Post by mrgooding »

Solved it - missing brackets!
Post Reply