Home > Article > Backend Development > Why Does `bindParam` Fail with Constant Values in PDO, and How Can I Fix It?
Troubleshooting "Cannot Pass Parameter by Reference" Error in bindParam
When utilizing bindParam with a constant value like null or empty strings, you may encounter this perplexing error. The culprit lies in the fundamental difference between bindParam and bindValue.
bindParam vs. bindValue
bindParam expects a variable parameter passed by reference, while bindValue directly assigns a value to a placeholder. This distinction becomes critical when working with constant values.
Solution: Use bindValue
To resolve the error, replace bindParam with bindValue. For null values, use the following syntax:
$stmt->bindValue(':param', null, PDO::PARAM_INT);
Note:
Attempting to bind a null value using bindParam with PDO::PARAM_NULL may not work for all users. bindValue has proven to be a more reliable solution.
The above is the detailed content of Why Does `bindParam` Fail with Constant Values in PDO, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!