<?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>Shoultes.net &#187; MySQL</title>
	<atom:link href="http://shoultes.net/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://shoultes.net</link>
	<description>Seth Shoultes PHP Website</description>
	<lastBuildDate>Tue, 31 Aug 2010 05:58:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>WordPress dbDelta Function Problems</title>
		<link>http://shoultes.net/wordpress-dbdelta-function-problems/</link>
		<comments>http://shoultes.net/wordpress-dbdelta-function-problems/#comments</comments>
		<pubDate>Sun, 07 Mar 2010 07:43:44 +0000</pubDate>
		<dc:creator>Seth</dc:creator>
				<category><![CDATA[My Plugins]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Code Samples]]></category>
		<category><![CDATA[Ramblings]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Wordpress Plugins]]></category>

		<guid isPermaLink="false">http://shoultes.net/?p=1808</guid>
		<description><![CDATA[For a couple of weeks I was trying to figure out why the database tables in my plugin weren&#8217;t getting updated when the plugin was installed or activated. I had recently written a function (based on this example) to create tables in my WordPress plugin. I finally narrowed it down to the dbDelta function for [...]]]></description>
			<content:encoded><![CDATA[<p>For a couple of weeks I was trying to figure out why the database tables in my <a href="../wordpress-events-registration-with-paypal-ipn/">plugin</a> weren&#8217;t getting updated when the plugin was installed or activated. I had recently written a <a href="http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/">function</a> (based on this <a href="http://codex.wordpress.org/Creating_Tables_with_Plugins" target="_blank">example</a>) to <a href="http://codex.wordpress.org/Creating_Tables_with_Plugins" target="_blank">create tables</a> in my <a href="http://wordpress.org/extend/plugins/advanced-events-registration/" target="_blank">WordPress plugin</a>. I finally narrowed it down to the dbDelta function for WordPress. After doing  a few <a href="http://www.google.com/search?q=dbDelta+not+working" target="_blank">searches on Google</a> I came across this <a href="http://hungred.com/how-to/wordpress-dbdelta-function/" target="_blank">article </a>which explains the the dbDelta function in detail.</p>
<p>Come to find out I was missing a space between a &#8216;<span style="color: #ff0000;">&#8220;</span>&#8216; and a &#8216;<span style="color: #ff0000;">(</span>&#8216; as seen below.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql_create_table</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;CREATE TABLE &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$wp_table_name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;( &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$sql</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; );&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here is how it should have looked:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql_create_table</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;CREATE TABLE &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$wp_table_name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;  (  &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$sql</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;  )  ;&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Notice the spaces highlighted in green? That was the killer. So for a while, every time I added a new field to a table in database install file. For a while I was using a function (seen below) to alter the table and add the new fields.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> add_column_if_not_exist<span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #339933;">,</span> <span style="color: #000088;">$column</span><span style="color: #339933;">,</span> <span style="color: #000088;">$column_attr</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;VARCHAR( 255 ) NULL&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$exists</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$columns</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>query<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;show columns from <span style="color: #006699; font-weight: bold;">$db</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>get_row<span style="color: #009900;">&#40;</span><span style="color: #000088;">$columns</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Field'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$column</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$exists</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$exists</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>query<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ALTER TABLE `<span style="color: #006699; font-weight: bold;">$db</span>` ADD `<span style="color: #006699; font-weight: bold;">$column</span>`  <span style="color: #006699; font-weight: bold;">$column_attr</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'There was a problem adding columns to the database.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$error</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So, if you are having trouble with the dbDelta function when writing a Wrodpress plugin. Be aware of extra spaces <img src='http://shoultes.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley colorbox-1808' /> </p>
<p>Here is more information about the dbDelta function and creating tables with plugins:</p>
<p><a href="http://codex.wordpress.org/Creating_Tables_with_Plugins" target="_blank">http://codex.wordpress.org/Creating_Tables_with_Plugins</a></p>
<p><a href="http://wordpress.org/tags/dbdelta-1" target="_blank">http://wordpress.org/tags/dbdelta-1</a></p>
<p><a href="http://hungred.com/how-to/wordpress-dbdelta-function/" target="_blank">http://hungred.com/how-to/wordpress-dbdelta-function/</a></p>
<p><a href="http://designoplasty.com/2009/05/15/not-using-dbdelta-with-wordpress/" target="_blank">http://designoplasty.com/2009/05/15/not-using-dbdelta-with-wordpress/</a></p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Wordpress+dbDelta+Function+Problems+-+http://b2l.me/ugq7f&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://shoultes.net/wordpress-dbdelta-function-problems/&amp;t=Wordpress+dbDelta+Function+Problems" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://shoultes.net/wordpress-dbdelta-function-problems/&amp;title=Wordpress+dbDelta+Function+Problems" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://shoultes.net/wordpress-dbdelta-function-problems/&amp;title=Wordpress+dbDelta+Function+Problems" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://shoultes.net/wordpress-dbdelta-function-problems/&amp;title=Wordpress+dbDelta+Function+Problems" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://shoultes.net/wordpress-dbdelta-function-problems/&amp;title=Wordpress+dbDelta+Function+Problems" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-gmail">
			<a href="https://mail.google.com/mail/?ui=2&amp;view=cm&amp;fs=1&amp;tf=1&amp;su=Wordpress+dbDelta+Function+Problems&amp;body=Link: http://shoultes.net/wordpress-dbdelta-function-problems/ (sent via shareaholic)%0D%0A%0D%0A----%0D%0A For%20a%20couple%20of%20weeks%20I%20was%20trying%20to%20figure%20out%20why%20the%20database%20tables%20in%20my%20plugin%20weren%27t%20getting%20updated%20when%20the%20plugin%20was%20installed%20or%20activated.%20I%20had%20recently%20written%20a%20function%20%28based%20on%20this%20example%29%20to%20create%20tables%20in%20my%20Wordpress%20plugin.%20I%20finally%20narrowed%20it%20down%20to%20the%20dbDelta%20funct" rel="nofollow" class="external" title="Email this via Gmail">Email this via Gmail</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.google.com/bookmarks/mark?op=add&amp;bkmk=http://shoultes.net/wordpress-dbdelta-function-problems/&amp;title=Wordpress+dbDelta+Function+Problems" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-blogger">
			<a href="http://www.blogger.com/blog_this.pyra?t&amp;u=http://shoultes.net/wordpress-dbdelta-function-problems/&amp;n=Wordpress+dbDelta+Function+Problems&amp;pli=1" rel="nofollow" class="external" title="Blog this on Blogger">Blog this on Blogger</a>
		</li>
		<li class="shr-comfeed">
			<a href="http://shoultes.net/wordpress-dbdelta-function-problems/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.dzone.com/links/add.html?url=http://shoultes.net/wordpress-dbdelta-function-problems/&amp;title=Wordpress+dbDelta+Function+Problems&amp;description=For%20a%20couple%20of%20weeks%20I%20was%20trying%20to%20figure%20out%20why%20the%20database%20tables%20in%20my%20plugin%20weren%27t%20getting%20updated%20when%20the%20plugin%20was%20installed%20or%20activated.%20I%20had%20recently%20written%20a%20function%20%28based%20on%20this%20example%29%20to%20create%20tables%20in%20my%20Wordpress%20plugin.%20I%20finally%20narrowed%20it%20down%20to%20the%20dbDelta%20funct" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://shoultes.net/wordpress-dbdelta-function-problems/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-googlereader">
			<a href="http://www.google.com/reader/link?url=http://shoultes.net/wordpress-dbdelta-function-problems/&amp;title=Wordpress+dbDelta+Function+Problems&amp;srcUrl=http://shoultes.net/wordpress-dbdelta-function-problems/&amp;srcTitle=Wordpress+dbDelta+Function+Problems&amp;snippet=For%20a%20couple%20of%20weeks%20I%20was%20trying%20to%20figure%20out%20why%20the%20database%20tables%20in%20my%20plugin%20weren%27t%20getting%20updated%20when%20the%20plugin%20was%20installed%20or%20activated.%20I%20had%20recently%20written%20a%20function%20%28based%20on%20this%20example%29%20to%20create%20tables%20in%20my%20Wordpress%20plugin.%20I%20finally%20narrowed%20it%20down%20to%20the%20dbDelta%20funct" rel="nofollow" class="external" title="Add this to Google Reader">Add this to Google Reader</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://shoultes.net/wordpress-dbdelta-function-problems/&amp;title=Wordpress+dbDelta+Function+Problems&amp;summary=For%20a%20couple%20of%20weeks%20I%20was%20trying%20to%20figure%20out%20why%20the%20database%20tables%20in%20my%20plugin%20weren%27t%20getting%20updated%20when%20the%20plugin%20was%20installed%20or%20activated.%20I%20had%20recently%20written%20a%20function%20%28based%20on%20this%20example%29%20to%20create%20tables%20in%20my%20Wordpress%20plugin.%20I%20finally%20narrowed%20it%20down%20to%20the%20dbDelta%20funct&amp;source=Shoultes.net" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-mail">
			<a href="mailto:?subject=%22Wordpress%20dbDelta%20Function%20Problems%22&amp;body=Link: http://shoultes.net/wordpress-dbdelta-function-problems/ (sent via shareaholic)%0D%0A%0D%0A----%0D%0A For%20a%20couple%20of%20weeks%20I%20was%20trying%20to%20figure%20out%20why%20the%20database%20tables%20in%20my%20plugin%20weren%27t%20getting%20updated%20when%20the%20plugin%20was%20installed%20or%20activated.%20I%20had%20recently%20written%20a%20function%20%28based%20on%20this%20example%29%20to%20create%20tables%20in%20my%20Wordpress%20plugin.%20I%20finally%20narrowed%20it%20down%20to%20the%20dbDelta%20funct" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a>
		</li>
		<li class="shr-squidoo">
			<a href="http://www.squidoo.com/lensmaster/bookmark?http://shoultes.net/wordpress-dbdelta-function-problems/" rel="nofollow" class="external" title="Add to a lense on Squidoo">Add to a lense on Squidoo</a>
		</li>
		<li class="shr-yahoobuzz">
			<a href="http://buzz.yahoo.com/submit/?submitUrl=http://shoultes.net/wordpress-dbdelta-function-problems/&amp;submitHeadline=Wordpress+dbDelta+Function+Problems&amp;submitSummary=For%20a%20couple%20of%20weeks%20I%20was%20trying%20to%20figure%20out%20why%20the%20database%20tables%20in%20my%20plugin%20weren%27t%20getting%20updated%20when%20the%20plugin%20was%20installed%20or%20activated.%20I%20had%20recently%20written%20a%20function%20%28based%20on%20this%20example%29%20to%20create%20tables%20in%20my%20Wordpress%20plugin.%20I%20finally%20narrowed%20it%20down%20to%20the%20dbDelta%20funct&amp;submitCategory=business&amp;submitAssetType=text" rel="nofollow" class="external" title="Buzz up!">Buzz up!</a>
		</li>
		<li class="shr-yahoomail">
			<a href="http://compose.mail.yahoo.com/?Subject=Wordpress+dbDelta+Function+Problems&amp;body=Link: http://shoultes.net/wordpress-dbdelta-function-problems/ (sent via shareaholic)%0D%0A%0D%0A----%0D%0A For%20a%20couple%20of%20weeks%20I%20was%20trying%20to%20figure%20out%20why%20the%20database%20tables%20in%20my%20plugin%20weren%27t%20getting%20updated%20when%20the%20plugin%20was%20installed%20or%20activated.%20I%20had%20recently%20written%20a%20function%20%28based%20on%20this%20example%29%20to%20create%20tables%20in%20my%20Wordpress%20plugin.%20I%20finally%20narrowed%20it%20down%20to%20the%20dbDelta%20funct" rel="nofollow" class="external" title="Email this via Yahoo! Mail">Email this via Yahoo! Mail</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://shoultes.net/wordpress-dbdelta-function-problems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Function for Creating Tables with WordPress Plugins</title>
		<link>http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/</link>
		<comments>http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/#comments</comments>
		<pubDate>Sun, 07 Mar 2010 07:41:23 +0000</pubDate>
		<dc:creator>Seth</dc:creator>
				<category><![CDATA[My Plugins]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[PHP Code Samples]]></category>
		<category><![CDATA[PHP Developer News]]></category>
		<category><![CDATA[PHP Everywhere]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Wordpress Plugins]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[dbDelta]]></category>
		<category><![CDATA[installing tables]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[upgrade]]></category>

		<guid isPermaLink="false">http://shoultes.net/?p=1807</guid>
		<description><![CDATA[Here is a very useful function I have written to install/update the database tables in your custom WordPress plugin. Basically I have used the examples given on the &#8220;Creating Tables with Plugins&#8221; page at WordPress.org. In your main plugin file (ex. my_plugin.php) I define my plugin version: define&#40;&#34;MY_PLUGIN_VERSION&#34;, &#34;2.16&#34; &#41;; //Declare the plugin version. This [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a very useful function I have written to install/update the database tables in your custom WordPress plugin. Basically I have used the <a href="http://codex.wordpress.org/Creating_Tables_with_Plugins">examples</a> given on the &#8220;<a href="http://codex.wordpress.org/Creating_Tables_with_Plugins">Creating Tables with Plugins</a>&#8221; page at <a href="http://wordpress.org/">WordPress.org</a>.</p>
<p>In your main plugin file (ex. my_plugin.php) I define my plugin version:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MY_PLUGIN_VERSION&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;2.16&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Declare the plugin version. This way we know the tables are always up to date. I usually declare this in my main plugin file.</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;includes/functions.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;includes/database_install.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
register_activation_hook<span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'my_plugin_data_tables_install'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then in my functions.php file:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> my_plugin_run_install <span style="color: #009900;">&#40;</span><span style="color: #000088;">$table_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table_version</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		   <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
		   <span style="color: #000088;">$wp_table_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>prefix <span style="color: #339933;">.</span> <span style="color: #000088;">$table_name</span><span style="color: #339933;">;</span>
		   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>get_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SHOW TABLES LIKE '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$table_name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$table_name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$sql_create_table</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;CREATE TABLE &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$wp_table_name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; ( &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$sql</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; ) ;&quot;</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span>ABSPATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'wp-admin/includes/upgrade.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				dbDelta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql_create_table</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">//create option for table version</span>
				<span style="color: #000088;">$option_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$table_name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'_tbl_version'</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$newvalue</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$table_version</span><span style="color: #339933;">;</span>
				  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #000088;">$option_name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					    update_option<span style="color: #009900;">&#40;</span><span style="color: #000088;">$option_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newvalue</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: #000088;">$deprecated</span><span style="color: #339933;">=</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span>
					    <span style="color: #000088;">$autoload</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'no'</span><span style="color: #339933;">;</span>
					    add_option<span style="color: #009900;">&#40;</span><span style="color: #000088;">$option_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newvalue</span><span style="color: #339933;">,</span> <span style="color: #000088;">$deprecated</span><span style="color: #339933;">,</span> <span style="color: #000088;">$autoload</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				  <span style="color: #009900;">&#125;</span>
			<span style="color: #666666; font-style: italic;">//create option for table name</span>
				<span style="color: #000088;">$option_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$table_name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'_tbl'</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$newvalue</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wp_table_name</span><span style="color: #339933;">;</span>
				  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #000088;">$option_name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					    update_option<span style="color: #009900;">&#40;</span><span style="color: #000088;">$option_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newvalue</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: #000088;">$deprecated</span><span style="color: #339933;">=</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span>
					    <span style="color: #000088;">$autoload</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'no'</span><span style="color: #339933;">;</span>
					    add_option<span style="color: #009900;">&#40;</span><span style="color: #000088;">$option_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newvalue</span><span style="color: #339933;">,</span> <span style="color: #000088;">$deprecated</span><span style="color: #339933;">,</span> <span style="color: #000088;">$autoload</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				  <span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Code here with new database upgrade info/table Must change version number to work.</span>
	<span style="color: #000088;">$installed_ver</span> <span style="color: #339933;">=</span> get_option<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$table_name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'_tbl_version'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	     <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$installed_ver</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$table_version</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		  <span style="color: #000088;">$sql_create_table</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;CREATE TABLE &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$wp_table_name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; ( &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$sql</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; ) ;&quot;</span><span style="color: #339933;">;</span>
	      <span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span>ABSPATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'wp-admin/includes/upgrade.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	      dbDelta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql_create_table</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	      update_option<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$table_name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'_tbl_version'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table_version</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	      <span style="color: #009900;">&#125;</span>
	    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Using the function is quite simple and can save a few lines of code. especially if you need to install several new tables.</p>
<p>Here is an example of database_install.php:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> my_plugin_data_tables_install <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$table_version</span> <span style="color: #339933;">=</span> MY_PLUGIN_VERSION<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Call the plugin version.</span>
<span style="color: #666666; font-style: italic;">//Install the first table</span>
<span style="color: #000088;">$table_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;my_first_plugin_tbl&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;id mediumint(9) NOT NULL AUTO_INCREMENT,
	  time bigint(11) DEFAULT '0' NOT NULL,
	  name tinytext NOT NULL,
	  text text NOT NULL,
	  url VARCHAR(100) NOT NULL,
	  UNIQUE KEY id (id)&quot;</span><span style="color: #339933;">;</span>
my_plugin_run_install  <span style="color: #009900;">&#40;</span><span style="color: #000088;">$table_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table_version</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Install the second table</span>
<span style="color: #000088;">$table_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;my_second_plugin_tbl&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;id mediumint(9) NOT NULL AUTO_INCREMENT,
	   category_name VARCHAR(100) DEFAULT NULL,
	   category_identifier VARCHAR(45) DEFAULT NULL,
	   category_desc TEXT,
	   display_desc VARCHAR (4) DEFAULT NULL,
	  UNIQUE KEY id (id)&quot;</span><span style="color: #339933;">;</span>
my_plugin_run_install  <span style="color: #009900;">&#40;</span><span style="color: #000088;">$table_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table_version</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I hope this helps some of the WordPress plugin authors out there.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Function+for+Creating+Tables+with+Wordpress+Plugins+-+http://b2l.me/ugq7n&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/&amp;t=Function+for+Creating+Tables+with+Wordpress+Plugins" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/&amp;title=Function+for+Creating+Tables+with+Wordpress+Plugins" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/&amp;title=Function+for+Creating+Tables+with+Wordpress+Plugins" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/&amp;title=Function+for+Creating+Tables+with+Wordpress+Plugins" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/&amp;title=Function+for+Creating+Tables+with+Wordpress+Plugins" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-gmail">
			<a href="https://mail.google.com/mail/?ui=2&amp;view=cm&amp;fs=1&amp;tf=1&amp;su=Function+for+Creating+Tables+with+Wordpress+Plugins&amp;body=Link: http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/ (sent via shareaholic)%0D%0A%0D%0A----%0D%0A Here%20is%20a%20very%20useful%20function%20I%20have%20written%20to%20install%2Fupdate%20the%20database%20tables%20in%20your%20custom%20Wordpress%20plugin.%20Basically%20I%20have%20used%20the%20examples%20given%20on%20the%20%22Creating%20Tables%20with%20Plugins%22%20page%20at%20Wordpress.org.%0D%0A%0D%0AIn%20your%20main%20plugin%20file%20%28ex.%20my_plugin.php%29%20I%20define%20my%20plugin%20version%3A%0D%0Adefi" rel="nofollow" class="external" title="Email this via Gmail">Email this via Gmail</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.google.com/bookmarks/mark?op=add&amp;bkmk=http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/&amp;title=Function+for+Creating+Tables+with+Wordpress+Plugins" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-blogger">
			<a href="http://www.blogger.com/blog_this.pyra?t&amp;u=http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/&amp;n=Function+for+Creating+Tables+with+Wordpress+Plugins&amp;pli=1" rel="nofollow" class="external" title="Blog this on Blogger">Blog this on Blogger</a>
		</li>
		<li class="shr-comfeed">
			<a href="http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.dzone.com/links/add.html?url=http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/&amp;title=Function+for+Creating+Tables+with+Wordpress+Plugins&amp;description=Here%20is%20a%20very%20useful%20function%20I%20have%20written%20to%20install%2Fupdate%20the%20database%20tables%20in%20your%20custom%20Wordpress%20plugin.%20Basically%20I%20have%20used%20the%20examples%20given%20on%20the%20%22Creating%20Tables%20with%20Plugins%22%20page%20at%20Wordpress.org.%0D%0A%0D%0AIn%20your%20main%20plugin%20file%20%28ex.%20my_plugin.php%29%20I%20define%20my%20plugin%20version%3A%0D%0Adefi" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-googlereader">
			<a href="http://www.google.com/reader/link?url=http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/&amp;title=Function+for+Creating+Tables+with+Wordpress+Plugins&amp;srcUrl=http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/&amp;srcTitle=Function+for+Creating+Tables+with+Wordpress+Plugins&amp;snippet=Here%20is%20a%20very%20useful%20function%20I%20have%20written%20to%20install%2Fupdate%20the%20database%20tables%20in%20your%20custom%20Wordpress%20plugin.%20Basically%20I%20have%20used%20the%20examples%20given%20on%20the%20%22Creating%20Tables%20with%20Plugins%22%20page%20at%20Wordpress.org.%0D%0A%0D%0AIn%20your%20main%20plugin%20file%20%28ex.%20my_plugin.php%29%20I%20define%20my%20plugin%20version%3A%0D%0Adefi" rel="nofollow" class="external" title="Add this to Google Reader">Add this to Google Reader</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/&amp;title=Function+for+Creating+Tables+with+Wordpress+Plugins&amp;summary=Here%20is%20a%20very%20useful%20function%20I%20have%20written%20to%20install%2Fupdate%20the%20database%20tables%20in%20your%20custom%20Wordpress%20plugin.%20Basically%20I%20have%20used%20the%20examples%20given%20on%20the%20%22Creating%20Tables%20with%20Plugins%22%20page%20at%20Wordpress.org.%0D%0A%0D%0AIn%20your%20main%20plugin%20file%20%28ex.%20my_plugin.php%29%20I%20define%20my%20plugin%20version%3A%0D%0Adefi&amp;source=Shoultes.net" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-mail">
			<a href="mailto:?subject=%22Function%20for%20Creating%20Tables%20with%20Wordpress%20Plugins%22&amp;body=Link: http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/ (sent via shareaholic)%0D%0A%0D%0A----%0D%0A Here%20is%20a%20very%20useful%20function%20I%20have%20written%20to%20install%2Fupdate%20the%20database%20tables%20in%20your%20custom%20Wordpress%20plugin.%20Basically%20I%20have%20used%20the%20examples%20given%20on%20the%20%22Creating%20Tables%20with%20Plugins%22%20page%20at%20Wordpress.org.%0D%0A%0D%0AIn%20your%20main%20plugin%20file%20%28ex.%20my_plugin.php%29%20I%20define%20my%20plugin%20version%3A%0D%0Adefi" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a>
		</li>
		<li class="shr-squidoo">
			<a href="http://www.squidoo.com/lensmaster/bookmark?http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/" rel="nofollow" class="external" title="Add to a lense on Squidoo">Add to a lense on Squidoo</a>
		</li>
		<li class="shr-yahoobuzz">
			<a href="http://buzz.yahoo.com/submit/?submitUrl=http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/&amp;submitHeadline=Function+for+Creating+Tables+with+Wordpress+Plugins&amp;submitSummary=Here%20is%20a%20very%20useful%20function%20I%20have%20written%20to%20install%2Fupdate%20the%20database%20tables%20in%20your%20custom%20Wordpress%20plugin.%20Basically%20I%20have%20used%20the%20examples%20given%20on%20the%20%22Creating%20Tables%20with%20Plugins%22%20page%20at%20Wordpress.org.%0D%0A%0D%0AIn%20your%20main%20plugin%20file%20%28ex.%20my_plugin.php%29%20I%20define%20my%20plugin%20version%3A%0D%0Adefi&amp;submitCategory=business&amp;submitAssetType=text" rel="nofollow" class="external" title="Buzz up!">Buzz up!</a>
		</li>
		<li class="shr-yahoomail">
			<a href="http://compose.mail.yahoo.com/?Subject=Function+for+Creating+Tables+with+Wordpress+Plugins&amp;body=Link: http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/ (sent via shareaholic)%0D%0A%0D%0A----%0D%0A Here%20is%20a%20very%20useful%20function%20I%20have%20written%20to%20install%2Fupdate%20the%20database%20tables%20in%20your%20custom%20Wordpress%20plugin.%20Basically%20I%20have%20used%20the%20examples%20given%20on%20the%20%22Creating%20Tables%20with%20Plugins%22%20page%20at%20Wordpress.org.%0D%0A%0D%0AIn%20your%20main%20plugin%20file%20%28ex.%20my_plugin.php%29%20I%20define%20my%20plugin%20version%3A%0D%0Adefi" rel="nofollow" class="external" title="Email this via Yahoo! Mail">Email this via Yahoo! Mail</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://shoultes.net/function-for-creating-tables-with-wordpress-plugins/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
