The following results in an error because i cannot bind one var to multiple placeholders:
$search = "%somename%";
$stmt = $pdo->prepare("SELECT * FROM persons WHERE firstname LIKE :search OR lastname LIKE :search");
$stmt->bindValue(":search", $search, PDO::PARAM_STR);
$stmt->excecute();
my workaround is the following:
$search = "%somename%";
$search1 = $search;
$search2 = $search;
$stmt = $pdo->prepare("SELECT * FROM persons WHERE firstname LIKE :search1 OR lastname LIKE :search2");
$stmt->bindValue(":search1", $search1, PDO::PARAM_STR);
$stmt->bindValue(":search2", $search2, PDO::PARAM_STR);
$stmt->excecute();
I think it is not very efficient like this. I have to copy my var 2 times to be able to bind it 2 times. If i want to query 6 fields i need to copy it 6 times. My feeling is that there must be a better way.
Is there a better workaround for handling this case?