Maison  >  Article  >  développement back-end  >  PHP pdo 事宜无法回滚

PHP pdo 事宜无法回滚

WBOY
WBOYoriginal
2016-06-13 10:37:56737parcourir

PHP pdo 事务无法回滚
public function submit(){
try {
$this->beginTransaction();
$this->exec('update people set age=2');
$this->exec('INSERT INTO course (course,num) VALUES ("数学",100)');
$this->commit();
}catch (Exception $e){
$this->rollBack();
}
}

这是小弟的事务模块大致如上,数据库引擎为InnoDB,但不知道为何SQL如果执行错误,pdo始终不执行回滚,请高人指定,小弟感激不尽。。。

------解决方案--------------------
不要用try试试,我觉得这里不会抛出异常

PHP code
$this->beginTransaction();$this->exec('update people set age=2');//$step1 = mysql_error();//你的类里,改成如果执行sql错误,获取错误代码,相应的函数$this->exec('INSERT INTO course (course,num) VALUES ("数学",100)');//$step2 = mysql_error();if (!$step1 && !$step2){ $this->commit();}else{ $this->rollBack();}<br><font color="#e78608">------解决方案--------------------</font><br>同意2楼,<br>两条语句应该只会返回true或false<br><br>用if判断就可以了,,不要用 try<div class="clear">
                 
              
              
        
            </div>
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn