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.– rray
The original begin_transaction() method is only available from php 5.5, the server runs 5.3, so the refit :x.
– Wallace Magalhães
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.– rray
Sorry, I’ve already arranged the question. I’ll check that you said.
– Wallace Magalhães
Have another important detail your tables use Innodb engine?
– rray
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.
– Wallace Magalhães