<?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>Sean Gates' Blog &#187; MySQL</title>
	<atom:link href="http://www.seangates.com/category/web-development/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.seangates.com</link>
	<description>By the looks of it {he thinks} he knows what he's doing.</description>
	<lastBuildDate>Sat, 10 Jul 2010 21:22:04 +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>Sorting Associative Arrays in PHP</title>
		<link>http://www.seangates.com/2006/07/26/sorting-associative-arrays-in-php/</link>
		<comments>http://www.seangates.com/2006/07/26/sorting-associative-arrays-in-php/#comments</comments>
		<pubDate>Wed, 26 Jul 2006 18:10:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.orwd.com/blog/?p=16</guid>
		<description><![CDATA[IÃ‚Â encountered a problem yesterday (that flowed into today)Ã‚Â dealing with associative arrays and how to sort them.Ã‚Â My problem was that I had to merge two associative arrays, then sort them based on a value in the second level array. Here is an example: $my_array1 = array( Ã‚Â Ã‚Â Ã‚Â Ã‚Â array( Ã‚Â Ã‚Â [...]]]></description>
			<content:encoded><![CDATA[<p>IÃ‚Â encountered a problem yesterday (that flowed into today)Ã‚Â dealing with associative arrays and how to sort them.Ã‚Â  My problem was that I had to merge two associative arrays, then sort them based on a value in the second level array.<span id="more-16"></span></p>
<p>Here is an example:</p>
<blockquote><p><font face="Courier New">$my_array1 = array(<br />
Ã‚Â Ã‚Â Ã‚Â Ã‚Â  array(<br />
Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â  &#8216;id&#8217; => &#8217;1&#8242;,<br />
Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â  &#8216;name&#8217; => &#8216;tom&#8217;<br />
Ã‚Â Ã‚Â Ã‚Â Ã‚Â  ),<br />
Ã‚Â Ã‚Â Ã‚Â Ã‚Â  array(<br />
Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â  &#8216;id&#8217; => &#8217;2&#8242;,<br />
Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â  &#8216;name&#8217; => &#8216;harry&#8217;<br />
Ã‚Â Ã‚Â Ã‚Â Ã‚Â  )<br />
);<br />
</font><font face="Courier New">$my_array2 = array(<br />
Ã‚Â Ã‚Â Ã‚Â Ã‚Â  array(<br />
Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â  &#8216;id&#8217; => &#8217;5&#8242;,<br />
Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â  &#8216;name&#8217; => &#8216;dick&#8217;<br />
Ã‚Â Ã‚Â Ã‚Â Ã‚Â  ),<br />
Ã‚Â Ã‚Â Ã‚Â Ã‚Â  array(<br />
Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â  &#8216;id&#8217; => &#8217;4&#8242;,<br />
Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â  &#8216;name&#8217; => &#8216;bob&#8217;<br />
Ã‚Â Ã‚Â Ã‚Â Ã‚Â  )<br />
);<br />
</font><font face="Courier New">$merged_array = array_merge( $my_array1 , $my_array2 );</font></p></blockquote>
<p>Now, this part worked fine and dandy, except when I went to sort the merged array.Ã‚Â  I was using usort and a function I had found on the internet:</p>
<blockquote><p><font face="Courier New"><span class="keyword">function </span><span class="default">arfsort</span><span class="keyword">( </span><span class="default">$a</span><span class="keyword">, </span><span class="default">$fl </span></font><font face="Courier New"><span class="keyword">){<br />
Ã‚Â  </span><span class="default">$GLOBALS</span><span class="keyword">[</span><span class="string">'__ARFSORT_LIST__'</span><span class="keyword">] = </span><span class="default">$fl</span></font><font face="Courier New"><span class="keyword">;<br />
Ã‚Â  </span><span class="default">usort</span><span class="keyword">( </span><span class="default">$a</span><span class="keyword">, </span><span class="string">&#8216;arfsort_func&#8217; </span></font><font face="Courier New"><span class="keyword">);<br />
Ã‚Â  return </span><span class="default">$a</span></font><span class="keyword"><font face="Courier New">;<br />
}</font></span><span class="keyword"><font face="Courier New"><font face="Courier New"><span class="comment">// extended to allow sort direction per field sorted against<br />
</span><span class="keyword">function </span><span class="default">arfsort_func</span><span class="keyword">( </span><span class="default">$a</span><span class="keyword">, </span><span class="default">$b </span></font><font face="Courier New"><span class="keyword">){<br />
Ã‚Â  foreach( </span><span class="default">$GLOBALS</span><span class="keyword">[</span><span class="string">'__ARFSORT_LIST__'</span><span class="keyword">] as </span><span class="default">$f </span></font><font face="Courier New"><span class="keyword">) {<br />
Ã‚Â Ã‚Â  switch (</span><span class="default">$f</span><span class="keyword">[</span><span class="default">1</span><span class="keyword">]) { </span></font><font face="Courier New"><span class="comment">// switch on ascending or descending value<br />
Ã‚Â Ã‚Â  Ã‚Â  </span><span class="keyword">case </span><span class="string">&#8220;d&#8221;</span></font><font face="Courier New"><span class="keyword">:<br />
Ã‚Â Ã‚Â  Ã‚Â  Ã‚Â  </span><span class="default">$strc </span><span class="keyword">= </span><span class="default">strcmp</span><span class="keyword">( </span><span class="default">strtolower</span><span class="keyword">(</span><span class="default">$b</span><span class="keyword">[</span><span class="default">$f</span><span class="keyword">[</span><span class="default">0</span><span class="keyword">]]), </span><span class="default">strtolower</span><span class="keyword">(</span><span class="default">$a</span><span class="keyword">[</span><span class="default">$f</span><span class="keyword">[</span><span class="default">0</span></font><font face="Courier New"><span class="keyword">]]) );<br />
Ã‚Â Ã‚Â  Ã‚Â  Ã‚Â  if ( </span><span class="default">$strc </span><span class="keyword">!= </span><span class="default">0 </span></font><font face="Courier New"><span class="keyword">){<br />
Ã‚Â Ã‚Â  Ã‚Â  Ã‚Â  Ã‚Â  return </span><span class="default">$strc</span></font><font face="Courier New"><span class="keyword">;<br />
Ã‚Â Ã‚Â  Ã‚Â  Ã‚Â  }<br />
Ã‚Â Ã‚Â  Ã‚Â  break;<br />
Ã‚Â Ã‚Â  Ã‚Â  default:<br />
Ã‚Â Ã‚Â  Ã‚Â  Ã‚Â  </span><span class="default">$strc </span><span class="keyword">= </span><span class="default">strcmp</span><span class="keyword">( </span><span class="default">strtolower</span><span class="keyword">(</span><span class="default">$a</span><span class="keyword">[</span><span class="default">$f</span><span class="keyword">[</span><span class="default">0</span><span class="keyword">]]), </span><span class="default">strtolower</span><span class="keyword">(</span><span class="default">$b</span><span class="keyword">[</span><span class="default">$f</span><span class="keyword">[</span><span class="default">0</span></font><font face="Courier New"><span class="keyword">]]) );<br />
Ã‚Â Ã‚Â  Ã‚Â  Ã‚Â  if ( </span><span class="default">$strc </span><span class="keyword">!= </span><span class="default">0 </span></font><font face="Courier New"><span class="keyword">){<br />
Ã‚Â Ã‚Â  Ã‚Â  Ã‚Â  Ã‚Â  return </span><span class="default">$strc</span></font><font face="Courier New"><span class="keyword">;<br />
Ã‚Â Ã‚Â  Ã‚Â  Ã‚Â  }<br />
Ã‚Â Ã‚Â  Ã‚Â  break;<br />
Ã‚Â Ã‚Â  }<br />
Ã‚Â  }<br />
Ã‚Â  return </span><span class="default">0</span></font><span class="keyword"><font face="Courier New">;<br />
}</font></span></font></span></p></blockquote>
<p>What was happening was that the usort was not working.Ã‚Â  So, I started to think about ways to get around the problem of the usort.Ã‚Â  My first thoughts were some way to work around it in PHP, so I went and asked a colleague if she new of any way to sort associative arrays in the fashion I needed to.Ã‚Â  Her first comment was to try and do something in the SQL query before you ever get the data.Ã‚Â  We talked for a bit and I got this bright idea.Ã‚Â  I could use the handy IN operator from MySQL.Ã‚Â  First, I would have to build a string of IDs for the database, then use the IN operator to query for just those IDs in the database.Ã‚Â  For example:</p>
<blockquote><p><font face="Courier New">$my_array1 &#8230;. (like above)<br />
$my_array2 &#8230;. (like above)</font></p>
<p><font face="Courier New">$merged_array = array_merge( $my_array1 , $my_array2 );</font></p>
<p><font face="Courier New">foreach($merged_array as $m){<br />
Ã‚Â Ã‚Â Ã‚Â Ã‚Â  $merge_list[] = $m['id'];<br />
}</font></p>
<p><font face="Courier New">$list_for_query = implode(&#8216;,&#8217;,$merge_list);</font></p>
<p><font face="Courier New">$query = &#8220;SELECT * FROM my_table WHERE id IN ($merge_list) ORDER BY name&#8221;;</font></p></blockquote>
<p>So, to explain the above code it goes something like this:</p>
<ol>
<li>Make the arrays (mine were two different queries that I needed to merge together)</li>
<ul>
<li>Note: the arrays just need to have the row ID in them.Ã‚Â  No sense in wasting time getting all the columns from the table</li>
</ul>
<li>Merge the arrays (they need to be identical)</li>
<li>Make the associative arrays a one level array</li>
<li>Implode the list so there are commas separating the ID values</li>
<li>Query using those ID values and use ORDER BY to get the correct sorting</li>
</ol>
<p>Lo and behold, it works.Ã‚Â  It does just what I want, and I don&#8217;t have to fuss about using a custom usort.Ã‚Â  Hopefully this will help someone else down the road so you don&#8217;t waste your time like I did.</p>
<p>Ã‚Å </p>
]]></content:encoded>
			<wfw:commentRss>http://www.seangates.com/2006/07/26/sorting-associative-arrays-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL Replication</title>
		<link>http://www.seangates.com/2006/03/06/mysql-replication/</link>
		<comments>http://www.seangates.com/2006/03/06/mysql-replication/#comments</comments>
		<pubDate>Mon, 06 Mar 2006 18:33:34 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=5</guid>
		<description><![CDATA[A couple of months ago I started looking at how I could better manage the MySQL databases between the main PID server and our backup server. I found that MySQL has a nice builtin replication feature that allows real-time update to multiple backup servers without the need to do very much wrangling. MySQL Replication Documentation: [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of months ago I started looking at how I could better manage the MySQL databases between the main PID server and our backup server. I found that MySQL has a nice builtin replication feature that allows real-time update to multiple backup servers without the need to do very much wrangling.</p>
<p>MySQL Replication Documentation:<br />
<a href="http://dev.mysql.com/doc/refman/4.1/en/replication-howto.html">http://dev.mysql.com/doc/refman/4.1/en/replication-howto.html</a></p>
<p>One thing to note when setting up replication is that you <strong>must</strong> have the master and the slave on the same version. Other than that, if you follow the steps in the how-to you&#8217;ll be golden.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.seangates.com/2006/03/06/mysql-replication/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
