duanqiu9104 2012-12-29 00:59
浏览 20
已采纳

是否可以将PDO参数绑定到对象的成员变量?

Using PDOStatement::bindParam(), one can bind a parameter to a variable—which is especially useful when a prepared statement is executed multiple times, each with different parameter values. For example:

$dbh = new PDO('mysql:dbname=foo', 'eggyal', 'password1');
$qry = $dbh->prepare('DELETE FROM bar WHERE qux = ?');
$qry->bindParam(1, $qux, PDO::PARAM_INT);

while (true) {
  $qux = ... ;
  $qry->execute();
  // etc
}

My questions are:

  1. Is it possible to bind a parameter to the member variable of an object? For example:

    $qry->bindParam(1, $obj->qux, PDO::PARAM_INT);
    
  2. If so, to which object's member variable is such a parameter bound: that which is referenced at the time of the bindParam() call, or that which is referenced upon statement execution? For example:

    $obj->qux = 123;
    
    $obj = new stdClass();
    $obj->qux = 456;
    
    $qry->execute();  // which value is used for qux ?
    
  3. Where is this behaviour documented (if at all)?

  • 写回答

1条回答 默认 最新

  • duan5991518 2012-12-29 01:09
    关注

    PHP stores the reference of the variable to use it. When you call $qry->bindParam(1, $obj->qux, PDO::PARAM_INT), the reference stored is the reference of the member of the instanciated class.

    When you change the member $obj->qux, the reference is still the same than the one stored in your $obj. However, if you reinstanciate $obj to a new class, then every references are changed, but your old object is still in memory ! So when you assign a new value to the new $obj->qux, it is not the same variable used, so running $qry->execute will use the old value.

    I hope I've been clear enough.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀