PHP PDO:使用相同连接的多个预准备语句

时间:2011-08-03 00:37:06

标签: php pdo prepared-statement

我试图在同一个连接的循环中使用几个预准备语句,如下所示:

if (count($deleted) > 0) {
  $stmt_stock = $>conn->prepare('INSERT INTO `stock` 
     . (timestamp, id, qty) VALUES (' . $this->timestamp . ', ?, 0)');
  $stmt_stock->bindParam(1, $id);

  $stmt_status = $conn->prepare('INSERT INTO `status` '
     . '(timestamp, prev_timestamp, id) VALUES ('
     . $this->timestamp . ', ?, ?)');
  $stmt_status->bindParam(1, $o_ts);
  $stmt_status->bindParam(2, $id);

  $stmt_list = $conn->prepare('DELETE FROM `display_list` WHERE `id` = ?');
  $stmt_list->bindParam(1, $id);

  foreach($deleted as $id => $r) {  # $r is here values from $deleted
    $o_ts = $r[0];
    $stmt_stock->execute();
    $stmt_status->execute();
    $stmt_list->execute();
  }
}

这最终只会执行$stmt_status。但是,如果我从$stmt_list->execute();循环中注释掉foreach,则两个准备好的INSERT语句都按预期工作。将准备好的DELETE语句添加到此循环时,为什么循环失败,以及解决此问题的更好方法是什么?

0 个答案:

没有答案