Named Variable Binding in PDO in PHP
Similarly, you can perform
named variable binding via
bindValue. Let's see
how it's done. Suppose we have
the following variables:
<?php
$name = 'user';
$age = 25;
?>
Suppose we also have named placeholders:
<?php
$sql = 'SELECT * FROM users WHERE name=:name or age=:age';
$res = $pdo->prepare($sql);
?>
Let's bind variables to these placeholders.
To do this, the first parameter of the
bindValue method should specify
the names of the placeholders:
<?php
$res->bindValue('name', $name, PDO::PARAM_INT);
$res->bindValue('age', $age, PDO::PARAM_STR);
?>
Given variables:
<?php
$name1 = 'name1';
$name2 = 'name2';
?>
Get users whose name matches the value of the first or the second variable.
Given variables:
<?php
$age1 = 21;
$age2 = 22;
?>
Get users whose age matches the value of the first or the second variable.