Jump to content

Where if else cancel


Recommended Posts


I'm trying to use if in mysql query, but can't get it work.

So the case is i want to select rows when siteid is not zero, if it's zero then I need to cancel the where.

I've tried

SELECT * FROM items WHERE IF(siteid > 0,$siteid,0) = $siteid

With this query I got good result when $siteid is not 0, but it returns empty when $siteid = 0, which actually select siteid = 0. What I need is no more where, so it should just returns all.


Could anybody help me here?




Link to comment
Share on other sites

You can either change your PHP code to just not include the WHERE clause if $siteid = 0 (better approach) or you could do as follows:


$query="SELECT *
FROM items
WHERE $siteid<>0 AND siteid=$siteid";



Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

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.