PDO bindParam not working in foreach

后端 未结 2 505
我寻月下人不归
我寻月下人不归 2020-12-29 17:56

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

相关标签:
2条回答
  • 2020-12-29 18:24

    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.

    0 讨论(0)
  • 2020-12-29 18:32

    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

    0 讨论(0)
提交回复
热议问题