Jump to content

WHERE / AND/ OR


cK

Recommended Posts


$query = "SELECT * FROM test WHERE (TO_DAYS(NOW()) - TO_DAYS(timestamp)) < days_valid and category_id=\'$category[id]\' or category_id=\'9\' ORDER BY timestamp DESC LIMIT $pager->offset,$pager->limit";

$result = mysql_query($query) or error ("Unable to connect to SQL server. Try again later.");

 

Will the above query give all valid (=still public) results from category $category[id] or category 9 ?

 

I\'m not sure how to write it; first and or first or? with () or without?

Link to comment
https://forums.phpfreaks.com/topic/501-where-and-or/
Share on other sites

according to your query, it will select everything from test that has a specific id and not older than a certain number of days OR everything in category #9 (no matter how old its content is). Is this what you want?

 

You should try this; it will ensure all content is not older than a certain number of days, but might be coming from a specific category or from category #9:

$query = "SELECT * FROM test WHERE (TO_DAYS(NOW()) - TO_DAYS (timestamp)) < days_valid AND (category_id=\'$category[id]\' or category_id=\'9\') ORDER BY timestamp DESC LIMIT $pager->offset,$pager->limit";

// notice the use of ( and ) for the OR operator...

Use need to know how to use logical operators... This might help:

http://java.sun.com/docs/books/tutorial/ja...ts/bitwise.html

 

JP.

Link to comment
https://forums.phpfreaks.com/topic/501-where-and-or/#findComment-1704
Share on other sites

Archived

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

×
×
  • 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.