<?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; php Snippets</title>
	<atom:link href="http://www.beginnercode.com/category/computing/programming/php/php-snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.beginnercode.com</link>
	<description>The adventures of an amateur coder</description>
	<lastBuildDate>Thu, 15 Apr 2010 20:17:16 +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>0</slash:comments>
		</item>
		<item>
		<title>REGEX Library</title>
		<link>http://www.beginnercode.com/2006/03/09/regex-library/</link>
		<comments>http://www.beginnercode.com/2006/03/09/regex-library/#comments</comments>
		<pubDate>Thu, 09 Mar 2006 19:23:13 +0000</pubDate>
		<dc:creator>^Lestat</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php Snippets]]></category>

		<guid isPermaLink="false">http://www.beginnercode.com/?p=91</guid>
		<description><![CDATA[I was working up a form and needed to come up with an email regex. I know by now I probably should have my own regex library, or even be able to write my own &#8211; but I haven&#8217;t exercised my regex skills enough. Often time doesn&#8217;t allow an extensive learning period when your already [...]]]></description>
			<content:encoded><![CDATA[<p>I was working up a form and needed to come up with an email regex. I know by now I probably should have my own regex library, or even be able to write my own &#8211; but I haven&#8217;t exercised my regex skills enough. Often time doesn&#8217;t allow an extensive learning period when your already half way through creating a form! </p>
<p>I came across this great <a href="http://regexlib.com/default.aspx">regex library at regexlib.com</a>. It&#8217;s searchable, offers on the fly tests of user contibuted regular expresssions, rss feeds of recently added patterns, and contains a <a href="http://regexlib.com/CheatSheet.aspx">Cheat Sheet</a> for regex reference.</p>
<p>Of course, programmer beware. Be careful copying and pasting code. And definately test that code before you implement it.</p>
<p><tags>regex, php</tags></p>
]]></content:encoded>
			<wfw:commentRss>http://www.beginnercode.com/2006/03/09/regex-library/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
