pdo事务回滚的有关问题

WBOY
Release: 2016-06-13 11:54:22
Original
925 people have browsed it

pdo事务回滚的问题

本帖最后由 manbudezhu 于 2014-04-10 17:33:03 编辑 try {
        $dsn="mysql:host=localhost;dbname=dadao;port:3366";
        $pdo = new PDO($dsn, "root", "");
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $pdo->begintransaction();
        $sql="insert into user(username) values(:username)";
        $stmt = $pdo->prepare($sql);
        $stmt->execute(array(':username'=>'insert title1'));
        $stmt->execute(array(':username'=>NULL));
        $pdo->commit();

    } catch (Exception $e) {
        echo '事务异常,现在进行回滚';
        echo $e->getMessage();
        // 回滚事务
        $pdo->rollBack();
    }
一个简单的事务代码,数据库表user中username不允许为空。提示:事务异常,现在进行回滚 Column 'username' cannot be null1,数据库表没有写入。但是,将$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);删除后,就没有任何提示,且数据库中有记录插入。$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);这行代码不是设置的是错误的提示方式吗,为啥会影响数据库的插入。
------解决方案--------------------
是的,是设置错误处理方式
没有 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try  ...  catch 结构不会生效
$pdo->rollBack(); 永远执行不到
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!