<?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>Beginnercode.com &#187; MySQL</title>
	<atom:link href="http://www.beginnercode.com/category/computing/programming/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.beginnercode.com</link>
	<description>The adventures of an amateur coder</description>
	<lastBuildDate>Wed, 27 Apr 2011 20:07:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<image>
  <link>http://www.beginnercode.com</link>
  <url>http://www.beginnercode.com/wp-content/themes/neuron/favicon.ico</url>
  <title>Beginnercode.com</title>
</image>
		<item>
		<title>MySQL Conditional Insert</title>
		<link>http://www.beginnercode.com/2007/12/20/mysql-conditional-insert/</link>
		<comments>http://www.beginnercode.com/2007/12/20/mysql-conditional-insert/#comments</comments>
		<pubDate>Thu, 20 Dec 2007 22:13:05 +0000</pubDate>
		<dc:creator>^Lestat</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php Snippets]]></category>

		<guid isPermaLink="false">http://www.beginnercode.com/index.php/2007/12/20/mysql-conditional-insert/</guid>
		<description><![CDATA[I came across a situation where I needed to pull records from 1 db to another on a recurring basis. I read many different ways to do it via searching around the net. Some of the suggestions included creating a temporary table and copying it over. What was lacking was that sometimes the new table [...]]]></description>
			<content:encoded><![CDATA[<p>I came across a situation where I needed to pull records from 1 db to another on a recurring basis. I read many different ways to do it via searching around the net. Some of the suggestions included creating a temporary table and copying it over. What was lacking was that sometimes the new table also needed to be UPDATED, as the information changed from the original table.</p>
<p>I&#8217;ve come up with a dirty little example to show this can be done. There are <i>more</i> keywords that can enhance the <b><a href="http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html">ON DUPLICATE KEY UPDATE</a></b> function even more. The Example is assuming an &#8220;employees&#8221; table that looks like so:</p>
<table style="border-collapse:collapse;border:1px solid #000;">
<tr>
<th>employees</th>
</tr>
<tr>
<td style ="border:1px solid #000;">id (PK)</td>
</tr>
<tr>
<td style="border:1px solid #000;">first_name</td>
</tr>
</table>
<p>For brevity, I&#8217;ve left out connection data etc;</p>
<pre class="brush: php;">
$myArray = array(0 =&gt; array('id' =&gt; '1', 'fname' =&gt; 'Steve'),
                      array('id' =&gt; '2', 'fname' =&gt; 'sara'),
                      array('id' =&gt; '3', 'fname' =&gt; 'Matt')
                 );

// Don't forget to validate &amp; clean your data
// Connect to db here 

foreach($myArray as $key =&gt; $value){
          $query = &quot;INSERT INTO employees
          (id, first_name) VALUES ('$value[id]', '$value[fname]')
          ON DUPLICATE KEY UPDATE first_name = '$value[fname]'&quot;;

          $result = mysql_query($query);

          if(!$result){

                    print(&quot;Problem: &quot; . mysql_error() . &quot;&quot;);
          } else {

                    print(&quot;Success !&quot;);
          }

}
</pre>
<p>Read the manual on this for more information. This snippet will INSERT if a UNIQUE (in this case &#8216;id&#8217;) does not exist. It will UPDATE any existing unique. In this case any existing &#8216;id&#8217;s, the &#8216;first_name&#8217; column will get updated. </p>
<p>One could also use the <b><a href="http://dev.mysql.com/doc/refman/5.0/en/replace.html">REPLACE</a></b> function. As I understand the difference, REPLACE will DELETE any matching uniques, and INSERT a new record in it&#8217;s place.</p>
<p>Comments? Better ways? I&#8217;m always up for learning something new so please chime in ;-)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.beginnercode.com/2007/12/20/mysql-conditional-insert/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

