Created
June 30, 2013 22:37
-
-
Save 5iDS/5897274 to your computer and use it in GitHub Desktop.
MySQL database transaction, using the WordPress database object $wpdb. When you render a page in WordPress (front end or admin area), the $wpdb database object has already been initialised and opened up a connection to the database. Therefore we can recycle this database connection for our own needs. The $wpdb object saves the database handle as…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
global $wpdb; | |
// @ prefix used to suppress errors, but you should do your own | |
// error checking by checking return values from each mysql_query() | |
// Start Transaction | |
@mysql_query("BEGIN", $wpdb->dbh); | |
// Do some expensive/related queries here | |
$wpdb->query("DELETE FROM table WHERE form_id = '1' "); | |
$wpdb->query("DELETE FROM data WHERE form_id = '1' "); | |
if ($error) { | |
// Error occured, don't save any changes | |
@mysql_query("ROLLBACK", $wpdb->dbh); | |
} else { | |
// All ok, save the changes | |
@mysql_query("COMMIT", $wpdb->dbh); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment