Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WHERE clause confusion with PDO

Tags:

php

mysql

pdo

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.

like image 921
Greg Avatar asked Oct 20 '25 22:10

Greg


1 Answers

Try...

$stmt = $dbh->prepare("SELECT * FROM registry WHERE firstname = :name OR lastname = :name;");
$stmt->bindParam(':name', $name); 
like image 163
Jonno Avatar answered Oct 23 '25 13:10

Jonno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!