bindParam and bindValue
bindParam() binds by reference (reads the variable at execute time). bindValue() binds the current value immediately. bindValue is usually preferable.
bindParam() binds by reference (reads the variable at execute time). bindValue() binds the current value immediately. bindValue is usually preferable.
<?php
$stmt = $pdo->prepare("INSERT INTO products (name, price, stock) VALUES (:name, :price, :stock)");
// bindValue: bind the current value
$stmt->bindValue(":name", $productName, PDO::PARAM_STR);
$stmt->bindValue(":price", $price, PDO::PARAM_STR); // PDO has no PARAM_FLOAT
$stmt->bindValue(":stock", $stock, PDO::PARAM_INT);
$stmt->execute();
// bindParam: binds by reference (useful in loops)
$stmt = $pdo->prepare("INSERT INTO tags (name) VALUES (:name)");
$stmt->bindParam(":name", $tagName, PDO::PARAM_STR);
foreach ($tags as $tagName) {
$stmt->execute(); // Uses current value of $tagName
}
Prefer execute([...]) over bindParam/bindValue for simple cases — it is terser and less error-prone.