"OR" in SQL Query

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
easinewe
Forum Newbie
Posts: 23
Joined: Mon Jan 11, 2010 3:22 pm

"OR" in SQL Query

Post by easinewe »

I would like to select 2 different variables from my database. I wrote it like this:

$sql = "SELECT * FROM `Catalog` WHERE title = '{$article['alsolike']}' OR '{$article['alsolike2']}' ";

That does not seem to work.

The idea would be that it would output as such:

$sql = "SELECT * FROM `Catalog` WHERE title = 'Germany' OR 'Ireland' ";

How would this be written correctly?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: "OR" in SQL Query

Post by requinix »

OR in SQL works just like OR in every other programming language out there.

Code: Select all

condition OR condition OR condition...
"Ireland" is not a condition.
User avatar
SimpleManWeb
Forum Commoner
Posts: 57
Joined: Wed Dec 30, 2009 4:15 pm
Location: New Hampshire, USA

Re: "OR" in SQL Query

Post by SimpleManWeb »

"Ireland" is not a condition
To further elaborate,

Code: Select all

 
     $sql = "SELECT * FROM `Catalog` WHERE title = 'Germany' OR `title` = 'Ireland' ";
 
for more advanced queries, you will probably want to break your conditions up using parenthesis.

For your first query, use this:

Code: Select all

 
     $sql = "SELECT * FROM `Catalog` WHERE title = '".$article['alsolike']."' OR `title` = '".$article['alsolike2']."' ";
 
Hope this helps
easinewe
Forum Newbie
Posts: 23
Joined: Mon Jan 11, 2010 3:22 pm

Re: "OR" in SQL Query

Post by easinewe »

Thanks, that makes sense.

I suffer from a condition called Ireland. I'm learning to deal with it.
Post Reply