简体   繁体   English

PHP PDO错误消息返回,但回滚不起作用

[英]PHP PDO Error message returned, but rollback doesn't work

I'm using this class to handle my pdo connection and database actions, however I can't get the rollback function to work. 我正在使用此类来处理我的pdo连接和数据库操作,但是我无法使用回滚功能。 I create an intentional error at the $pers-query, get the following error: 我在$ pers-query中创建一个故意错误,得到以下错误:

( ! ) Warning: PDO::query(): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'webbshop.prson' doesn't exist in C:\wamp\www\webbshop\includes\db_con.php on line 21

but the other 2 querys are still executed, added to the database, and not rolled back. 但是其他2个查询仍将执行,添加到数据库中,并且不会回滚。 How do I do to get it all to work? 我该怎么做才能使一切正常工作?

I read at another post that you need to use InnoDB so I ran the SHOW ENGINES sql command, and it said that support for InnoDB was default and the comment said: "Supports transactions, row-level locking, and foreign keys" 我在另一篇文章中读到,您需要使用InnoDB,所以我运行了SHOW ENGINES sql命令,它说对InnoDB的支持是默认的,并且注释说:“支持事务,行级锁定和外键”

the PDO connection class: PDO连接类:

<?php
   class DB{

private $db_host = "localhost";
private $db_usr = "root";
private $db_pass = "";
private $db_name = "webbshop";
private $db;

function __construct(){
    $this->db = new PDO('mysql:host=' . $this->db_host . ';' 
    .'dbname=' . $this->db_name, $this->db_usr, $this->db_pass);
    $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}

function error(){

}   €$€$£

function Trans(){
    $this->db->beginTransaction();
}

function insert($sql){
    $stmt = $this->db->query($sql);
}

function fetch($sql){
    $stmt = $this->db->prepare($sql);
    $stmt->execute();
    return $stmt->fetchAll();
}

function lastInsertID() {
    return $this->db->lastInsertId();
}

function commitTrans(){
    $this->db->commit();
}

function rollback() {
    $this->db->rollBack();
}

function __destruct() {
    $this->db = null;
}
}

And this is the code I use to execute the querys: 这是我用来执行查询的代码:

<?php
require 'db_con.php';

try {
    $db = new DB();
    $db->Trans();
    $db->insert("INSERT INTO `webbshop`.`user` (`userID`, `nick`, `pass`) VALUES (NULL, '$_POST[nick]', '$_POST[pass]')");
    $nickID = $db->lastInsertID();
    echo $nickID;

    $pers = "INSERT INTO `webbshop`.`prson` (`personID`, `userID`, `fname`, `lname`, `persnr`, `email`) VALUES (NULL, $nickID, '$_POST[firstname]', '$_POST[lastname]', '$_POST[personnr]','$_POST[email]')";
    $addr = "INSERT INTO `webbshop`.`address` (`addressID`, `userID`, `street`, `city`, `zip`) VALUES (NULL, $nickID, '$_POST[address]', '$_POST[city]', '$_POST[zip]')";

    $db->insert($pers);
    $db->insert($addr);

    $db->commitTrans();
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "</br>";
    $db->rollback();
}

Are you using InnoDB? 您在使用InnoDB吗? To use transactions you should use InnoDB or rollback won't work! 要使用事务,您应该使用InnoDB或回滚将不起作用!

EDIT: 编辑:

try to change PDO::ERRMODE_WARNING to PDO::ERRMODE_EXCEPTION 尝试将PDO :: ERRMODE_WARNING更改为PDO :: ERRMODE_EXCEPTION

This problem make me crazy and I want to help u with my solution.The main problem is we call transaction from another class. 这个问题使我发疯,我想为您提供解决方案。主要问题是我们从另一个类调用事务。

The important thing is using InnoDB and setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)and lastly your connection have to be open singleton function. 重要的是使用InnoDB和setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION),最后,您的连接必须打开单例函数。

My code is: 我的代码是:

class PDO_db{ protected static $_conn; PDO_db {类,受保护的静态$ _conn;

--[ CONNECTION ]-- -[连接]-

public function __construct(){
    if(empty(self::$_conn)){
        try{
            self::$_conn = new PDO("mysql:host=".DB_SERVER.";dbname=".DB_NAME, DB_USERNAME, DB_PASSWORD, array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES 'UTF8'"));
            self::$_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }catch(PDOException $e){
            die("No Connection :: ".DB_NAME." : ".$e->getMessage());
        }
    }else{
        return self::$_conn;
    }
}

--[ TRANSACTION ]-- -[交易]-

public static function Trans(){
    return self::$_conn->beginTransaction();
}

--[ COMMIT ]-- -[提交]-

public static function Commit(){
    return self::$_conn->commit();
}

--[ ROLLBACK ]-- -[回滚]-

public static function Rollback(){
    return self::$_conn->rollBack();
}

--[ INSERT ]-- - [ 插入 ] -

public static function Insert($tablo, $rows){
}

} }

class db{ db类{

public function __construct(){
    $this->prodb = new PDO_db();
}

public function datainsert(){
    $this->prodb->Trans();
    try{
        $datainsert = $this->prodb->Insert($table, $data);
        $this->prodb->Commit();
        $message = "Insert True";
        return $message;
    }catch(Exception $ex){
        $this->prodb->Rollback();
        $message = "Insert Flase";
        return $message;
    }   
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM