AllThisOnAnACER
AllThisOnAnACER

Reputation: 331

PDO WHERE clause not working with OR

I'm using this and it works fine.

$stmt1 = $conn->prepare("SELECT * FROM Authors WHERE First=:id");
$stmt1->bindParam(':id', $_GET['name'], PDO::PARAM_STR);
blah blah...

It also works when I change WHERE 'First=:id' to 'Last=:id' but it fails when I include an OR clause as below.

$stmt1 = $conn->prepare("SELECT * FROM Authors WHERE First=:id OR Last=:id");
$stmt1->bindParam(':id', $_GET['name'], PDO::PARAM_STR); 

I found this below in Stack #3030650.

$stmt = $dbh->prepare("SELECT * FROM REGISTRY WHERE firstname = :name OR lastname = :name");
$stmt->bindParam(':name', $name);; 

Since these appear the same, what am I overlooking.

Thanks for any advice.

Upvotes: 1

Views: 1050

Answers (1)

meda
meda

Reputation: 45490

I would bind it twice, perhaps with different placeholders even though it's the same value:

$query = "SELECT * FROM REGISTRY WHERE firstname = :fname OR lastname = :lname";
$stmt = $dbh->prepare($query);
$stmt->bindParam(':fname', $name); 
$stmt->bindParam(':lname', $name);

Upvotes: 2

Related Questions