Question

I am using old-style mysql_query() functions to perform a transaction. It is not rolling back even though one of the queries in the transaction has failed. I have debugged affected rows and they are either 0 or 1. Syntax followed:

 $cnx = mysql_connect( .. ); 
 mysql_select_db("DB", $cnx); 
 mysql_query("START TRANSACTION");
 mysql_query("BEGIN");

 $isrollback = -1; 

 for (...) // run through query list 
 {
    mysql_query(".... query_i ....");
    if(mysql_affected_rows() == 0) 
    {
          $isrollback = 1
    }

 }

 // more queries 
 if ($isrollback > 0) 
     mysql_query("ROLLBACK");
 else
     mysql_query("COMMIT); 
Was it helpful?

Solution

You need to use a transactional database engine. eg INNODB

You are currently using MyISAM which does not support transactions.

This means you cannot start or rollback transactions without changing your DB engine.

MySQL gives instructions on how to convert from MyISAM to INNODB

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top