Hello, Because of the PHP version on the server, the fetch all and mysqli begin_transaction methods do not work. To solve this, I created another connection class by extending the MySQLi class and instantiating 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 mysqli-> rollback () method when a given 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?