PDO – bindParams vs. bindValues

Posted by & filed under OOP.

I was trying to supply binding params to a PDO query like this:

$a = array(
    ':firstname' => 'Mark',
    ':surname' => 'Flint'
);

foreach ($a as $k => $v) {
    $stmt->bindParam($, $v);
}

However, this will not work. From the PHP manual – “Unlike PDOStatement::bindValue(), the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute() is called.”

So, the correct way to do this would be:

foreach ($a as $k => $v) {
    $stmt->bindValue($k, $v);
}

See also, this link.

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Bitnami