Page 1 of 1

mysqli->rollback problem

Posted: Thu May 20, 2010 3:04 am
by derekzxy
Hi all,


I am new to the mysqli, I'm trying to create this function to rollback some queries if one of them has error.

however I found that the mysqi->rollback function doesn't rollback my query. please help.

here are the code I coded:
function executeSqlStatment($sql_statment_array)
{
$mysqli = new mysqli("xxx", "xxx", "xxx", "xxx" );

/* check connection */
if (mysqli_connect_errno())
{
writeLog("prepareSqlStatment", "Connect failed: ".mysqli_connect_error());
exit();
}
$is_query_error = false;
/* disable autocommit */
$mysqli->autocommit(FALSE);

for($i = 0; $i < count($sql_statment_array); $i++)
{
writeLog("prepareSqlStatment", $sql_statment_array[$i]);
if (!$mysqli->query($sql_statment_array[$i]))
{

$is_query_error = true;
writeLog("prepareSqlStatment", "Error: ".$mysqli->error);
}
}
if ($is_query_error)
{
$mysqli->rollback();
$mysqli->close();
writeLog("prepareSqlStatment", "Error: Rollback ". count($sql_statment_array) ." Statement ");
return false;

}
else
{
$mysqli->commit();
$mysqli->close();
return true;
}



}

THANKS GUYS : )