combining AND / OR statements

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
taldos
Forum Commoner
Posts: 39
Joined: Mon Aug 23, 2004 8:47 am
Location: Philadelphia

combining AND / OR statements

Post by taldos »

Hey all,

Just a quick question regarding AND / OR statement. I am using the following code in my mysql query to find matching rows to the following query.

Code: Select all

$alert = "SELECT id, email, first_name, last_name, lead_alert FROM members WHERE 
(location LIKE '%".$country."%' OR location='All')
AND (industry LIKE '%".$industry."%' OR industry='All')";

$a_sql = mysql_query($alert)
	or die ("Could not select matching members");
No matter how many times I scratch my head I can't figure it out. the fields specified (industry and location) contain more than one word of text. I don't think this has anything to do with the problem since LIKE uses patter matching, but just thought I would ask.

I am pretty sure it has something to do with how I am using the 'OR' statement combined with AND. Any help is appreciated.

Cheers.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're requiring all records to match the inputs of $country and $industry or be 'All' ?

What's the logic you want? What are you currently getting in records? What do you want in records?
taldos
Forum Commoner
Posts: 39
Joined: Mon Aug 23, 2004 8:47 am
Location: Philadelphia

Post by taldos »

If my table resembles the following:

Code: Select all

name     industry     location

tom        food          US
joe         finance     Japan
don         All            England
leon        fitness      US
mike        All            All
and I run a search for members whose industry= food/all AND country=US/all

the following records should be returned

Code: Select all

1. tom      food    us
2  mike     all       all
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try

Code: Select all

$alert = "SELECT id, email, first_name, last_name, lead_alert FROM members WHERE
(location LIKE '%".$country."%' OR industry LIKE '%".$industry."%')
OR (location='All' AND industry='All')";
taldos
Forum Commoner
Posts: 39
Joined: Mon Aug 23, 2004 8:47 am
Location: Philadelphia

Post by taldos »

thanks, but that statement would not meet the required parameters as a user would be selected if either his location or industry matched. The requirements are for both to match. Think I got i licked thought. thanks

Cheers.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

:oops: oops.. I meant to change that first OR to an AND..
Post Reply