Jump to content

Archived

This topic is now archived and is closed to further replies.

bagnallc

where statement

Recommended Posts

i have variables in my php code which are set or not set dependent upon the users preferences.

as it is unknown which ones will be set, they all have to include an AND command

so for example these could be some variables

$dog1="AND dog.dog_id IN $users_selection1"
$dog2="AND dog.dog_colour $users_selection2"
$dog3="AND dog.dog_height $users_selection3"

i then have a mysql_query which is as follows (i wont bother with all the php code as its irrelevant, but basically it turns the variables into part of the statement if set by user)
at the moment i put in an sql command which effectively selects all

SELECT pets.petnumber, dog.dog_name
FROM pets
JOIN dog ON pets.dog_id=dog.dog_id
WHERE pets.petnumber is not null $dog1 $dog2 $dog3

So if you looked for dogs that are brown the actual sql command would become

SELECT pets.petnumber, dog.dog_name, cat.cat_name
FROM pets
JOIN dog ON pets.dog_id=dog.dog_id
JOIN cat ON pets.cat_id=cat.cat_id
WHERE pets.petnumber is not null AND dog.dog_colour='brown'


what i want is an alternative way to the "pets.petnumber is not null" example in the above, so in other words a way of skipping straight on to the AND parts of the WHERE statement

any ideas?

Share this post


Link to post
Share on other sites
Huh?  You don't want to have that in your WHERE clause? I'm confused.

Share this post


Link to post
Share on other sites
i actually have the answer now.
if i put a 1 in it works fine

sorry if didnt explain well

Share this post


Link to post
Share on other sites
Oh... I understand... you wanted to be able to add the entire where clause in pieces.  I usually use "1=1" as the base piece.

Share this post


Link to post
Share on other sites

×

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.