01Jun2010

WordPress Plugin Tip – MySQL Transactions with $wpdb

This tip is more for WordPress theme and plugin developers. I’ve been working on a client project that required MySQL transactions, as I needed to perform multiple deletions on multiple tables, where the data was interdependent. Since I was writing a WordPress plugin, I wanted a way of doing a MySQL database transaction, using the WordPress database object $wpdb.

When you create a connection to a MySQL database, you end up with a connection handle. This is essentially a way of communicating with the database down the same pipe. 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 $wpdb->dbh (dbh = Database Handle). Therefore we just use $wpdb->dbh in place of our normal database handle that we’d otherwise use in PHP.

Code Example

<?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);
}
 
?>

Based on this, you can do your usual PHP and WordPress magic to do what you need.

Author
Dan Harrison

About the Author

Dan has been creating websites since 2003, and is 100% self-taught. Through lots of trial and error, Dan has learnt how to create successful websites, sharing his knowledge on RunningAWebsite.com. Dan is also a highly experienced UK-based Wordpress Developer, offering a range of specliased Wordpress Developer Services through his agency WP Doctors.

Dan Harrison has written 37 articles on Running A Website.

Sharing is caring.
  • Subscribe to our feed
  • Share this post on Delicious
  • StumbleUpon this post
  • Share this post on Digg
  • Tweet about this post
  • Share this post on Mixx
  • Share this post on Technorati
  • Share this post on Facebook
  • Share this post on NewsVine
  • Share this post on Reddit
  • Share this post on Google
  • Share this post on LinkedIn

Discussion

8 responses to "WordPress Plugin Tip – MySQL Transactions with $wpdb"

  • I get absolutely no errors at all – but the database tables do not get inserted into the database. I feel like I’m just overlooking some… thing. I hate that.

  • Agus Suhanto says:

    Thanks Dan for the tip. As far as I know, WP has WP_Error to handle with error reporting. On your code above, you use $error variable. It it on purpose?

  • Dan Harrison says:

    Hi Agus, I’m aware of WP’s error class, however, the use of $error was used for explaining the point, rather than being executable. Additionally, WP’s error class handles reporting, it doesn’t do any clean up after an operation, which is what “if $error” represents.

    Regards
    Dan

  • Nice approach… However, there’s something that makes me think.

    i’ve maaaany years that do not follow the improvements on MySQL.. so I might be totally wrong.

    AFAIK, WP tables uses the MyISAM engine which, as far as I remember, is not a transactional engine like InnoDB so… what’s the point of creating a transaction to handle data in non-transactional tables?

    Forgime if I’m totally wrong… I’m based on what I can remember from the early years of MySQL :)

  • Dan Harrison says:

    You can change the DB type if you want… but you need to do it in MySQL.

    Dan

  • marcus says:

    Plugin devs would want to set this when creating/updating tables during activation.

    however, why wouldn’t you just do this

    @$wpdb->query(“BEGIN TRANSACTION”);

    supposedly in 3.1 or later we’ll be able to do $wpdb->begin(), $wpdb->update(), etc.

  • Martin van der Poel says:

    Nice approach!

    For the record:

    BEGIN and BEGIN WORK are supported as aliases of START TRANSACTION for initiating a transaction. START TRANSACTION is standard SQL syntax and is the recommended way to start an ad-hoc transaction.

    Source: http://dev.mysql.com/doc/refman/5.0/en/commit.html

  • Dan Harrison says:

    Nice tip, thanks :)

Leave a Comment