<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Running A Website &#187; wordpress plugins</title>
	<atom:link href="http://www.runningawebsite.com/tag/wordpress-plugins/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.runningawebsite.com</link>
	<description>Practical tips and advice for running a successful website!</description>
	<lastBuildDate>Thu, 07 Oct 2010 10:20:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>WordPress Plugin Tip &#8211; MySQL Transactions with $wpdb</title>
		<link>http://www.runningawebsite.com/wordpress-plugin-tip-mysql-transactions-with-wpdb/</link>
		<comments>http://www.runningawebsite.com/wordpress-plugin-tip-mysql-transactions-with-wpdb/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 09:00:09 +0000</pubDate>
		<dc:creator>Dan Harrison</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[wordpress plugins]]></category>
		<category><![CDATA[wordpress themes]]></category>

		<guid isPermaLink="false">http://www.runningawebsite.com/?p=1000</guid>
		<description><![CDATA[This tip is more for WordPress theme and plugin developers. I&#8217;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 ...]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.runningawebsite.com%2Fwordpress-plugin-tip-mysql-transactions-with-wpdb%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.runningawebsite.com%2Fwordpress-plugin-tip-mysql-transactions-with-wpdb%2F&amp;source=DanJHarrison&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><img src="http://www.runningawebsite.com/wp-content/uploads/2010/05/Wordpress-Logo-Newspaper-Style-150x150.jpg" alt="" title="Wordpress Logo Newspaper Style" width="150" height="150" class="alignleft size-thumbnail wp-image-1002" /></p>
<p>This tip is more for WordPress theme and plugin developers. I&#8217;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 <strong>MySQL database transaction</strong>, <strong>using the WordPress database object</strong> <a href="http://codex.wordpress.org/Function_Reference/wpdb_Class">$wpdb</a>.</p>
<p><span id="more-1000"></span></p>
<p>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 <strong>$wpdb</strong> 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. </p>
<p>The $wpdb object saves the database handle as <strong>$wpdb->dbh</strong> (<strong>dbh</strong> = <strong>D</strong>ata<strong>b</strong>ase <strong>H</strong>andle). Therefore we just use <strong>$wpdb->dbh</strong> in place of our normal database handle that we&#8217;d otherwise use in PHP.</p>
<h3>Code Example</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?php
<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// @ prefix used to suppress errors, but you should do your own</span>
<span style="color: #666666; font-style: italic;">// error checking by checking return values from each mysql_query()</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Start Transaction</span>
<span style="color: #339933;">@</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;BEGIN&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Do some expensive/related queries here</span>
<span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DELETE FROM table WHERE form_id = '1' &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DELETE FROM data WHERE form_id = '1' &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$error</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Error occured, don't save any changes</span>
    <span style="color: #339933;">@</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ROLLBACK&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">// All ok, save the changes</span>
   <span style="color: #339933;">@</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;COMMIT&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Based on this, you can do your usual PHP and WordPress magic to do what you need. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.runningawebsite.com/wordpress-plugin-tip-mysql-transactions-with-wpdb/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

