PHP Transactions

Transactions can also be executed at the PHP level.

<!DOCTYPE html><html><head></head><body>

<?php
$S=new mysqli("localhost","root","password","testDB");
$S->query("DROP TABLE IF EXISTS tbl");
$S->query("CREATE TABLE tbl (a INT)");
$S->begin_transaction();
   $S->query("INSERT INTO tbl VALUES(100)");
$S->rollback();

$S->autocommit(FALSE);
   $S->query("INSERT INTO tbl VALUES(200)");
//$S->commit();
$S->autocommit(TRUE); // also commits transaction

tabulate($S->query("SELECT * FROM tbl"));

// ****** A useful, general-purpose result printer
function tabulate($result){
   $fInfo = $result->fetch_fields();
   echo "<table border='1'><tr>";
   foreach($fInfo as $col){
      echo "<td>".$col->name."</td>";
   }
   while ($row = $result->fetch_row()){
      echo "</tr><tr>";
      foreach ($row as $val){
	     echo "<td>".$val."</td>";
	  }
   }
   echo "</tr></table><br/>";
}
?>
</body>
</html>

a
200