Mike Giordano
Mike Giordano

Reputation: 19

SQL WHERE Clause with OR

I'm trying to add a record "name2" to this line of code, so the page will display articles for name1 or name2 users. This is the current code, which only displays articles for name1 users.

$table = ARTICLE;
if (isContributor()) {
$sql = "SELECT * FROM $table WHERE name1=".$_SESSION['user_id'];

This code works and displays articles for name2 users:

$sql = "SELECT * FROM $table WHERE name2=".$_SESSION['user_id'];

And when I want to display the articles for both users, I use the following code, but it breaks the page:

$sql = "SELECT * FROM $table WHERE name1 OR name2=".$_SESSION['user_id'];

Can you provide me with the correct line of code?

Upvotes: 1

Views: 62

Answers (2)

Talha Quddoos
Talha Quddoos

Reputation: 556

Try changing

$sql = "SELECT * FROM $table WHERE name1 OR name2=".$_SESSION['user_id'];

to

$sql = "SELECT * FROM $table WHERE name1=".$_SESSION['user_id']." OR name2=".$_SESSION['user_id'];

Upvotes: 1

dnoeth
dnoeth

Reputation: 60462

This ORed condition can be rewritten using IN, probably

$sql = "SELECT * FROM $table WHERE ".$_SESSION['user_id']" IN (name1,name2)";

Upvotes: 3

Related Questions