php中mysql事务回滚无效怎么办

PHPz
Release: 2020-09-05 10:11:49
Original
1666 people have browsed it

php中mysql事务回滚无效是因为数据库引擎设置为“MyISAM”了,该引擎是不支持事务回滚的,所以产生事务回滚无效,其解决办法就是将引擎更换为“InnoDB”即可恢复正常。

php中mysql事务回滚无效怎么办

php中mysql事务回滚无效怎么办?

我的测试环境:AppServ

<?php 
    header("Content-type:text/html;charset=utf-8");
    $pdo = new PDO(&#39;mysql:host=localhost;dbname=data&#39;,&#39;root&#39;,&#39;root&#39;);
    $pdo->exec(&#39;set names utf8&#39;);
    $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);#修改报错模式
  #开启事务
    $pdo->beginTransaction();
    try{
        #第一次操作
        $sql = &#39;delete from books where bId=40&#39;;
        $smt = $pdo->prepare($sql);
        $smt->execute();
        #第二次操作
        $sql = &#39;delete from1 books where bId=39&#39;;
        $smt = $pdo->prepare($sql);
        $smt->execute();
        #成功提交
        $pdo->commit();
    }catch(PDOException $e){
        echo $e->getMessage().&#39;<br>&#39;.$e->getLine();
        #失败撤回
        $pdo->rollBack();
    }
 ?>
Copy after login

如上:第二次操作出错的情况下 第一条操作没有被回滚

这时候赶紧去看看你的数据库引擎,你肯定看到是MyISAM,很不幸这个引擎不支持事务回滚。切换到 InnoDB 即可 。

企业微信截图_15926227684879.png

更多相关技术文章,请访问PHP中文网

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!