当我使用具有常量值的 bindParam 时,为什么会出现无法通过引用传递参数 2 错误? [英] Why I am getting Cannot pass parameter 2 by reference error when I am using bindParam with a constant value?

查看:18
本文介绍了当我使用具有常量值的 bindParam 时,为什么会出现无法通过引用传递参数 2 错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码,我感到非常沮丧:

I'm using this code and I'm beyond frustration:

try {
    $dbh = new PDO('mysql:dbname=' . DB . ';host=' . HOST, USER, PASS);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
}
catch(PDOException $e)
{
    ...
}
$stmt = $dbh->prepare('INSERT INTO table(v1, v2, ...) VALUES(:v1, :v2, ...)');
$stmt->bindParam(':v1', PDO::PARAM_NULL); // --> Here's the problem

PDO::PARAM_NULL, null, '', 都失败并抛出这个错误:

PDO::PARAM_NULL, null, '', all of them fail and throw this error:

致命错误:无法在/opt/... 中通过引用传递参数 2

Fatal error: Cannot pass parameter 2 by reference in /opt/...

推荐答案

你需要使用bindValue,而不是bindParam

bindParam 通过引用获取一个变量,并且在调用 bindParam 时不取值.我在 PHP 文档的评论中发现了这一点:

bindParam takes a variable by reference, and doesn't pull in a value at the time of calling bindParam. I found this in a comment on the PHP docs:

bindValue(':param', null, PDO::PARAM_INT);

附言您可能很想这样做 bindValue(':param', null, PDO::PARAM_NULL); 但它并不适合所有人(感谢 Will Shaver 的报告.)

P.S. You may be tempted to do this bindValue(':param', null, PDO::PARAM_NULL); but it did not work for everybody (thank you Will Shaver for reporting.)

这篇关于当我使用具有常量值的 bindParam 时,为什么会出现无法通过引用传递参数 2 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆