PHP Tutorial - PHP mysqli_autocommit() Function






Definition

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

Syntax

PHP mysqli_autocommit() Function has the following syntax.

mysqli_autocommit(connection,mode);

Parameter

ParameterIs RequiredDescription
connectionRequired.MySQL connection
modeRequired.FALSE turns auto-commit off. TRUE turns auto-commit on

Return

It returns TRUE on success or FALSE on failure.

The mysqli_commit() function commits the current transaction. The mysqli_rollback() function rolls back the current transaction.





Example

The following code turns off auto-committing, make some queries, then commit the queries.


<?php/*from  w w  w.jav a  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 (FirstName)VALUES ('PHP')");
mysqli_query($con,"INSERT INTO emp (FirstName)VALUES ('Java')");

// Commit transaction
mysqli_commit($con);

mysqli_close($con);
?>