我有这个密码:
$this->db->beginTransaction();
$query = 'INSERT INTO `table1` VALUES (NULL,:a,:b,NULL,NOW())';
$sth = $this->db->prepare($query);
foreach ($values as $k => $v) {
$sth->bindParam(':' . $k, $v, PDO::PARAM_INT);
}
$this->executeQueryRollbackOnException($sth, 'Message_1');
$resetId = $this->db->lastInsertId();
$query = 'UPDATE `table2` SET c=:c,d=:d WHERE reset_id IS NULL';
$sth = $this->db->prepare($query);
foreach ($values2 as $k => $v) {
$sth->bindParam(':' . $k, $v, PDO::PARAM_INT);
}
$this->executeQueryRollbackOnException($sth, 'Message_2');
Zend_Debug::dump($sth->rowCount(), 'Affected'); // This is 0
// Commit
$this->db->commit();
...
private function executeQueryRollbackOnException($sth, $message) {
try {
$sth->execute();
} catch (Exception $e) {
$this->logSQLError($e);
$this->db->rollBack();
throw new Exception($message);
}
}
第一个查询已执行,但第二个查询未执行。没有产生mysql错误。有什么想法吗?
最佳答案
我找到了解决办法。
foreach ($values as $k => $v) {
$sth->bindParam(':' . $k, $v, PDO::PARAM_INT);
}
应该是
foreach ($values as $k => $v) {
$sth->bindParam(':' . $k, $values[$k], PDO::PARAM_INT);
}
抱歉,我第一次没有发布正确的代码,我排除了foreaches来简化代码
关于php - PHP PDO事务中的多个准备语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10087878/