Skip to content

Instantly share code, notes, and snippets.

@saidqb
Forked from nciske/wpdb-transactions
Created September 13, 2020 01:23
Show Gist options
  • Save saidqb/676603c3d4b1e8c5f0f01940e7cdd35e to your computer and use it in GitHub Desktop.
Save saidqb/676603c3d4b1e8c5f0f01940e7cdd35e to your computer and use it in GitHub Desktop.
MySQL database transaction, using the WordPress database object $wpdb. Requires the InnoDB table format.
<?php
global $wpdb;
// Start Transaction
$wpdb->query( "START TRANSACTION" );
// Do some expensive/related queries here
//$wpdb->query("DELETE FROM table WHERE form_id = '1' ");
//$wpdb->query("DELETE FROM data WHERE form_id = '1' ");
// set $error variable value in error handling after $wpdb modifications
if ($error) {
// Error occured, don't save any changes
$wpdb->query( "ROLLBACK" );
} else {
// All ok, save the changes
$wpdb->query( "COMMIT" );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment