PHP Tutorial - PHP mysqli_rollback() Function






Definition

The mysqli_rollback() function rolls back the current transaction for the specified database connection.

Syntax

PHP mysqli_rollback() Function has the following syntax.

Object oriented style

bool mysqli::rollback ([ int $flags [, string $name ]] )

Procedural style

bool mysqli_rollback ( mysqli $link [, int $flags [, string $name ]] )

Parameter

  • link - Procedural style only: A link identifier returned by mysqli_connect() or mysqli_init()
  • flags - A bitmask of MYSQLI_TRANS_COR_* constants.
  • name - If provided then ROLLBACK/*name*/ is executed.




Return

It returns TRUE on success. FALSE on failure.

Example

The mysqli_commit() function commits the current transaction for the specified database connection.

The mysqli_autocommit() function turns on or off auto-committing database.


<?php/*  www  .  j a va 2 s  .  c  o m*/
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno($con)){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// Set autocommit to off
mysqli_autocommit($con,FALSE);

// Insert some values 
mysqli_query($con,"INSERT INTO emp (name)VALUES ('Java')");
mysqli_query($con,"INSERT INTO emp (name)VALUES ('PHP')");

// Commit transaction
mysqli_commit($con);

// Rollback transaction
mysqli_rollback($con);

mysqli_close($con);
?>