Home  >  Article  >  Backend Development  >  PHP pdo 事宜无法回滚

PHP pdo 事宜无法回滚

WBOY
WBOYOriginal
2016-06-13 13:16:361393browse

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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn