Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP mysql_查询事务不回滚_Php_Mysql_Transactions - Fatal编程技术网

PHP mysql_查询事务不回滚

PHP mysql_查询事务不回滚,php,mysql,transactions,Php,Mysql,Transactions,不久前,我编写了一个数据库类,我在大多数项目中都使用它,最近还扩展了该类以处理事务 下面的事务类似乎无法正常工作,服务器是MySQL innoDB,我已经检查了事务是否在我的数据库上工作(使用PHPMyAdmin)。所以这显然是我的代码中的错误或者我的误解 <?php /** * Description of Database * * @author http://stackoverflow.com/users/820750/gerve */ class Database {

不久前,我编写了一个数据库类,我在大多数项目中都使用它,最近还扩展了该类以处理事务

下面的事务类似乎无法正常工作,服务器是MySQL innoDB,我已经检查了事务是否在我的数据库上工作(使用PHPMyAdmin)。所以这显然是我的代码中的错误或者我的误解

<?php

/**
 * Description of Database
 *
 * @author http://stackoverflow.com/users/820750/gerve
 */
class Database {

    private $connection;

    public function __construct($dbServer, $dbName, $dbUser, $dbPassword) {
        $this->connection = mysql_connect($dbServer, $dbUser, $dbPassword);
        mysql_select_db($dbName, $this->connection);
    }

    public function query($query, $die = true) {
        return new Query($query, $this->connection, $die);
    }

    public function escape($param) {
        return mysql_real_escape_string($param, $this->connection);
    }

    public function transaction() {
        return new Transaction($this->connection);
    }

}

class Query {

    private $query;
    public $count;
    public $countModified;
    public $queryString;


    public function __construct($query, $link_identifier, $die = true) {
        if($die){
            $this->query = mysql_query($query, $link_identifier) or die(mysql_error());
        }
        else{
            $this->query = mysql_query($query, $link_identifier);
        }

        $this->count = is_resource($this->query) ? mysql_num_rows($this->query) : 0;
        $this->countModified = mysql_affected_rows($link_identifier);

        $this->queryString = $query;
    }

    public function nextRow() {
        return mysql_fetch_object($this->query);
    }

    public function allRows() {
        $array = array();

        while($row = mysql_fetch_object($this->query)){
            $array[] = $row;
        }

        return $row;
    }

}

class Transaction {

    private $connection;
    private $isAlive;

    public function __construct($link_identifier) {
        $this->connection = $link_identifier;
        $this->query("BEGIN");
        $this->isAlive = true;
    }

    public function query($query) {
        if($this->isAlive){
            $q = new Query($query, $this->connection, false);
            if(mysql_error()){
                $this->rollBack();
                return false;
            }
            else{
                return $q;
            }
        }
    }

    public function rollBack() {
        if($this->isAlive){
            $this->query("ROLLBACK");
            $this->isAlive = false;
        }
    }

    public function commit() {
        if($this->isAlive){
            $this->query("COMMIT");
            $this->isAlive = false;
            return true;
        }
        else{
            return false;
        }
    }

}

?>
上面的代码不应该在数据库中插入任何行,因为第二个查询失败,所以没有一个应该成功


我的问题是它不会回滚,不管查询是否失败,都会插入行。

尝试使用
START TRANSACTION
而不是
BEGIN

顺便说一下,
START TRANSACTION
将允许您对一致快照使用

WITH CONSISTENT SNAPSHOT(具有一致性快照)选项可为具有一致性快照功能的存储引擎启动。这只适用于InnoDB


来源:

我发现代码中有一个错误,就在
\u构造()
中。很简单,我只是换了一行:

发件人:

public function __construct($link_identifier) {
    $this->connection = $link_identifier;
    $this->query("BEGIN");
    $this->isAlive = true;
}
public function __construct($link_identifier) {
    $this->connection = $link_identifier;
    $this->isAlive = true;
    $this->query("BEGIN");
}
至:

public function __construct($link_identifier) {
    $this->connection = $link_identifier;
    $this->query("BEGIN");
    $this->isAlive = true;
}
public function __construct($link_identifier) {
    $this->connection = $link_identifier;
    $this->isAlive = true;
    $this->query("BEGIN");
}

->isAlive是在第一次“BEGIN”查询之后设置的,这意味着BEGIN从未发送过。

请不要使用
mysql.*
函数来生成新代码。它们不再得到维护,社区已开始恢复。看到了吗?相反,你应该学习并使用或。如果你不能决定,将帮助你做出选择。如果你想学的话。只使用默认支持事务的东西,仍然是支持的特性。@ PeeHaa:谢谢,我们会考虑PDO。但我还是想暂时解决这个问题。