Current Location: Home> Function Categories> mysqli::commit

mysqli::commit

(mysqli_commit) Submit the current transaction
Name:mysqli::commit
Category:MySQLi
Programming Language:php
One-line Description:(mysqli_commit) Submit the current transaction

Definition and usage

The commit() / mysqli_commit() function is used to commit the current transaction for the specified database connection.

Tip: You can also view the autocommit() function, which is used to enable or disable the automatic commit function of database modifications, and the rollback() function, which is used to rollback the current transaction.

Example

Example 1 - Object-Oriented Style

Turn off automatic submission, execute some queries, and submit these queries:

 <?php
$mysqli = new mysqli ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( $mysqli -> connect_errno ) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error ;
  exit ( ) ;
}

// Turn off automatic submission
$mysqli -> autocommit ( FALSE ) ;

// Insert some values
$mysqli -> query ( "INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Peter','Griffin',35)" ) ;
$mysqli -> query ( "INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)" ) ;

// Submit transaction
if ( ! $mysqli -> commit ( ) ) {
  echo "Commit transaction failed" ;
  exit ( ) ;
}

$mysqli -> close ( ) ;
?>

Example 2 - Procedural Style

Turn off automatic submission, execute some queries, and submit these queries:

 <?php
$con = mysqli_connect ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( mysqli_connect_errno ( ) ) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error ( ) ;
  exit ;
}

// Turn off automatic submission
mysqli_autocommit ( $con , FALSE ) ;

// Insert some values
mysqli_query ( $con , "INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Peter','Griffin',35)" ) ;
mysqli_query ( $con , "INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)" ) ;

// Submit transaction
if ( ! mysqli_commit ( $con ) ) {
  echo "Commit transaction failed" ;
  exit ( ) ;
}

// Close the connection
mysqli_close ( $con ) ;
?>
Similar Functions
Popular Articles