tags:

views:

41

answers:

2

I'm having some trouble understanding how to use prepared statements, when you need to match one value against several columns at once.

In other words what instead of doing this:

$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name = ?");
$stmt->bindParam(1, $name); 

I wanted to do this:

$stmt = $dbh->prepare("SELECT * FROM REGISTRY where firstname = ? or lastname = ?");

with both '?' representing the same string.

+1  A: 

Try...

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

Jonno
Thank you kindly!
Greg
+1  A: 

Why not just use a named parameter:

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