Autocommit with PDO

£可爱£侵袭症+ 提交于 2020-05-31 04:05:21

问题


The rollback of my transaction doesn't work. How do I set autocommit to false (or 0) in the php script using PDO (I have InnoDB 5.7.18) ?

Here is my code:

     global $bdd;  //defined with PDO

       try {                             
            $bdd->beginTransaction();
            /* my requests */
            $bdd->commit();
        } catch (Exception $e) {
            $bdd->rollBack();
            return $e->getMessage();
        }

        return true;
    }

回答1:


 $db = new PDO('mysql:dbname=employee');
 $db->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
 var_dump($db->query('SELECT @@autocommit')->fetchAll());



回答2:


I solved my problem myself: a few of my tables were in MyISAM (whereas the majority are in InnoDB -> I work with an old database system...); so the rollback didn't work for these tables. Once I changed them into InnoDB, it worked. Thanks to everybody for the help !




回答3:


Try setting the following attribute:

$bdd->setAttribute(PDO::ATTR_AUTOCOMMIT,0);


来源:https://stackoverflow.com/questions/45234859/autocommit-with-pdo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!