codedude
codedude

Reputation: 6519

Can you have an OR in a WHERE statement inside a mysql query?

Is it possible to have an OR inside a WHERE statement in a mysql query as I have done below.

$query = mysql_query("SELECT * FROM fields WHERE post_id=$id OR post_id="" order by id desc") or die(mysql_error());

This produces a server error when run on my site. Any thoughts on how I could accomplish this?

Thanks in advance.

Upvotes: 0

Views: 86

Answers (3)

Ash
Ash

Reputation: 128

what are the errors.. as such ur query is fine but u string problems with all these double quotes.. try something like this..

$query = mysql_query("SELECT * FROM fields 
           WHERE post_id = " . $id . " OR post_id='' order by id desc") 
         or die(mysql_error());

Upvotes: -2

Mark Byers
Mark Byers

Reputation: 838186

Yes you can have an OR. What is the type of post_id?

If post_id is a character type:

"SELECT * FROM fields WHERE post_id='$id' OR post_id='' order by id desc"

If it's an integer then it can't be equal to the empty string. Did you mean post_id IS NULL instead?

Upvotes: 3

Hari Menon
Hari Menon

Reputation: 35405

What is the error? It looks like you have not escaped the double quote in the query. It should be:

$query = mysql_query("SELECT * FROM fields WHERE post_id=$id OR post_id=\"\" order by id desc") or die(mysql_error());

Upvotes: 0

Related Questions