I am using PDO
for an application but getting a problem for PDO bindParam()
. I have an array and I want to use the values of array for PDO bi
use bindValue()
instead of bindParam()
. bindParam()
binds to a reference, so when you execute the query all the parameters use the last value of $val
.
If you already have the items in an array, there's no reason to call $stmt->bindParam
in a loop; just do:
$con = $this->connection();
$stmt = $con->prepare($sql);
$stmt->execute($params);
$result = $stmt->fetchAll();
Per the PHP documentation:
Execute the prepared statement. If the prepared statement included parameter markers, you must either:
call PDOStatement::bindParam() to bind PHP variables to the parameter markers: bound variables pass their value as input and receive the output value, if any, of their associated parameter markers
or pass an array of input-only parameter values