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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀