Rollback method does not work in mysqli extended class

Asked

Viewed 107 times

2

Hello, Because of the PHP version on the server, the fetch all and begin_transaction methods of mysqli do not work. To solve this, I created another connection class extending the Mysqli class and started to instantiate it.

<?php
namespace App\DB;

class MySQLiConnector extends \mysqli
{
    function __construct($host, $user, $pass, $db)
    {
        parent::__construct($host, $user, $pass, $db);
    }

    public function query($query) {
        if( !$this->real_query($query) ) {
            return false;
        }

        return new MySQLiResultSet($this);
    }

    public function begin_transaction($flags = NULL, $name = NULL)
    {
        $this->autocommit(FALSE);
    }
}

and the mysqliresultset class

<?php
namespace App\DB;

class MySQLiResultSet extends \MySQLi_Result
{
    public function fetch()
    {
        return $this->fetch_assoc();
    }

    public function fetch_all()
    {
        $rows = array();
        while($row = $this->fetch())
        {
            $rows[] = $row;
        }
        return $rows;
    }
}

?>

However, when I try to use the method mysqli->rollback() when a query fails, it does not work, the changes are saved anyway.

The code where I try to run the rollback:

        $this->db->begin_transaction();

        $query1 = $this->db->query($sql1);
        $query2 = $this->db->query($sql2);
        $query3 = $this->db->query($sql3);


        if (!$query1 || !$query2 || !$query3) {
           $this->db->rollback();
           throw new ModelException("Erro da base de dados. -> ".$this->db->error);
        } else {
           $this->db->commit();
           return true;
        }

Is there any way to solve this problem?

  • You have overwritten the original method begin_transaction() of Mysqli, it may be that you have not even started the transfer, so maybe the changes will be made.

  • The original begin_transaction() method is only available from php 5.5, the server runs 5.3, so the refit :x.

  • Did not have this information in the question but okay, check if connection passed (where you apply autocommit false) is the same that retrieves the query (MySQLiResultSet) it seems that the object q vc applies the autocommit is different from the one that executes/retrieves the query.

  • Sorry, I’ve already arranged the question. I’ll check that you said.

  • 2

    Have another important detail your tables use Innodb engine?

  • 1

    Putz, I swore they used... but no, they use Mysam. That’s it, because I tested it on another basis with innodb tables and it worked. Thank you.

Show 1 more comment

1 answer

4


This Function doesn’t work with non Transactional table types (like Myisam or ISAM).

According to the PHP manual alert. Only tables with Innodb or NDB engine support logo transactions autocommit(FALSE) will not work with Myisam tables. On the Mysql website there is a comparative list of the functionalities of each engine.

Another feature that Innodb has and Myisam is the option of foreign keys with integrity reference.

Related:

Foreign Key does not respect referential integrity

Browser other questions tagged

You are not signed in. Login or sign up in order to post.