<?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>Zenovation's Blog &#187; mysql</title>
	<atom:link href="http://zenovations.com/blog/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://zenovations.com/blog</link>
	<description>A blog about web design, programming, hosting, and virtualmin hacks</description>
	<lastBuildDate>Mon, 16 Jan 2012 15:01:54 +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>Quick and Dirty Database Conversion Tool</title>
		<link>http://zenovations.com/blog/2009/04/quick-and-dirty-database-conversion-tool/</link>
		<comments>http://zenovations.com/blog/2009/04/quick-and-dirty-database-conversion-tool/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 19:11:29 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://zenovations.com/blog/?p=16</guid>
		<description><![CDATA[I created a quick tool for importing database data and modifying structure and data as it goes. It populates a single table at a time, offers decent flexibility, and allows you to specify your own callback functions to deal with data conversion. It can handle inserts and allows you to specify customized queries if desired. [...]]]></description>
			<content:encoded><![CDATA[<p>I created a quick tool for importing database data and modifying structure and data as it goes. It populates a single table at a time, offers decent flexibility, and allows you to specify your own callback functions to deal with data conversion.  It can handle inserts and allows you to specify customized queries if desired. It allows any complexity of joins on the source data. Alas, it only works in mysql because that&#8217;s all I needed it for.  Use an adapt for any non-evil purpose you like &#8211; Creative Commons.  Usage:</p>
<ol>
<li>Open configure.php</li>
<li>Set $fake_inserts = true (for testing)</li>
<li>Probably want to set $max_rows (for testing)</li>
<li>Set up your table conversions using the $tablemap arrays</li>
<li>Call the script to test results: php ./converter.php</li>
<li>When satisfied, set $fake_inserts = false (got a backup??)</li>
<li>Whammo!</li>
</ol>
<p>Here is the <b>config.php</b> file you&#8217;ll need to tweak:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p16code3'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p163"><td class="code" id="p16code3"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*************************************************************
   CONFIGURABLE PARAMETERS
**********************************************************/</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * These can also be configured per-query below, if more granularity is needed
 */</span>
<span style="color: #000088;">$from_db_connection</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'db-instance'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$to_db_connection</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'db-instance'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * We either output by printing insert statements to the screen for testing(true),
 * or by running the insert queries directly on the target db(false-default). Set this
 * to false when you're ready to import for real
 */</span>
<span style="color: #000088;">$fake_inserts</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Max rows allows you to only run a certain number of queries per table, for testing. Leave
 * this set to 0 for the real run
 */</span>
<span style="color: #000088;">$max_rows</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * If true, script will exist on any failed insert query.
 * If false, will continue to run even if queries fail and insert the other records.
 */</span>
<span style="color: #000088;">$stop_on_failed_query</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * $tablemap is an array containing one entry for each table to be converted.
 * Note that tables don't necessarily have to be 1:1 to make this work, since
 * the source query can use whatever sort of joins needed to set up the source
 * fields. There must, of course, be exactly one output table.
 *
 * &lt;p&gt;Here are the fields for each conversion element:
 * &lt;ul&gt;
 *   &lt;li&gt;connection info - an array containin the source database and target database. It contains two
 *       elements, each is an array containing array( host, username, password, db_instance )
 *   &lt;li&gt;source_query - the query to be run against source db, set it up however you like, the field names should correspond with the 'fields' array's keys
 *   &lt;li&gt;fields - see below
 *   &lt;li&gt;defaults - default values to apply to any fields in target table, overwritten by values in 'fields'
 * &lt;/ul&gt;
 *
 * &lt;p&gt;If you just want to create insert statements with the data, use dest_table. If you
 * need custom queries or updates to run, you can specify dest_query. Values for any output fields
 * can be substituted using {field_name} in the query.
 *
 * &lt;p&gt;The 'fields' element is an indexed array. The key is the source table's column name. The value is either a string indicating the name of hte column in the destination table, or an array containing exactly two elements: 1) a function to call, which will be passed the value from source table, 2) the destination column name
 *
 * &lt;p&gt;The function is passed a host of arguments, in order:
 * &lt;ol&gt;
 *  &lt;li&gt;value from originating column
 *  &lt;li&gt;entire data row
 *  &lt;li&gt;original column name
 *  &lt;li&gt;destination column name
 * &lt;/ol&gt;
 *
 * &lt;p&gt;To list the same field twice (since the keys would be identical) you can use an array index which will be stripped from the column name before querying, such as &quot;field_id[1]&quot;, &quot;field_id[2]&quot;, which both refer to a column named &quot;field_id&quot;
 */</span>
<span style="color: #000088;">$tablemap</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
* This example is a simple, straight copy from one table to another
*/</span>
<span style="color: #000088;">$tablemap</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
	 <span style="color: #0000ff;">&quot;connection_info&quot;</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
				    <span style="color: #0000ff;">&quot;source&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$from_db_connection</span><span style="color: #339933;">,</span>
				    <span style="color: #0000ff;">&quot;dest&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$to_db_connection</span>
	 <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	 <span style="color: #0000ff;">&quot;source_query&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'select * from source_table'</span><span style="color: #339933;">,</span>
	 <span style="color: #0000ff;">&quot;dest_table&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;destination_table&quot;</span><span style="color: #339933;">,</span>
	 <span style="color: #0000ff;">'fields'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'source1'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'dest1'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'source2'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'dest2'</span><span style="color: #339933;">,</span>
			   <span style="color: #0000ff;">'posted'</span>  <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'toSqlDate'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'date'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// convert unix timestamp to sql date</span>
			   <span style="color: #0000ff;">'id[1]'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// here we set two dest fields from &quot;id&quot;, using array index</span>
			   <span style="color: #0000ff;">'id[2]'</span>   <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'getParentId'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'parent'</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// another custom function</span>
	 <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	 <span style="color: #0000ff;">'defaults'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
			   <span style="color: #0000ff;">'dest3'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'0'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'dest4'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span>
	 <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
 * This example runs a customized query instead of a normal insert
 */</span>
&nbsp;
<span style="color: #000088;">$tablemap</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
	 <span style="color: #0000ff;">&quot;connection_info&quot;</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
          <span style="color: #0000ff;">&quot;source&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$from_db_connection</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">&quot;dest&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$to_db_connection</span>
	 <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	 <span style="color: #0000ff;">&quot;source_query&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'
         select 
            count(*) as src_total, category
         from 
            source_table_1 s1, source_table_2 s2
         where
            s1.id = s2.id AND s2.id &gt; 100
         group by
            category'</span><span style="color: #339933;">,</span>
	 <span style="color: #0000ff;">&quot;dest_query&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'update dest_table set total = {dest_total} where catid = {cat}'</span><span style="color: #339933;">,</span>
	 <span style="color: #0000ff;">'fields'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
			   <span style="color: #0000ff;">'category'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'cat'</span><span style="color: #339933;">,</span>
			   <span style="color: #0000ff;">'src_total'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'dest_total'</span>
	 <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	 <span style="color: #0000ff;">'defaults'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// these are still applicable, just didn't need any</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">/*****************************************************************************
  USER FUNCTIONS (methods used by the 'fields' array to convert values)
***************************************************************************/</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Just an example function that creates a value by combining two cols from source table
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> getParentId<span style="color: #009900;">&#40;</span><span style="color: #000088;">$src_id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data_row</span><span style="color: #339933;">,</span> <span style="color: #000088;">$src_col_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dest_col_name</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;">$src_id</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// if it exists, use it</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$src_id</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #666666; font-style: italic;">// otherwise, create one</span>
   <span style="color: #b1b100;">return</span> <span style="color: #000088;">$data_row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;number_1&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$data_row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;number_2&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Example function to create an incremental id for the result data
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> getIncrementalId<span style="color: #009900;">&#40;</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;">$currentIncId</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #339933;">++</span><span style="color: #000088;">$currentIncId</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$currentIncId</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Converts a unix timestamp to a sql datetime
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> toSqlDate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$utime</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <a href="http://www.php.net/date"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y-m-d H:i:s&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$utime</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></td></tr></table></div>

<p>And here is the <b>convert.php</b> script you&#8217;ll be running:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p16code4'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p164"><td class="code" id="p16code4"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #666666; font-style: italic;">/*********************************
 GO TO THIS FILE TO CONFIGURE
 ********************************/</span>
  <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;config.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*********************************
 YOU WON'T NEED TO CHANGE ANYTHING BELOW HERE
 ********************************/</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Prints out contents and adds html chars if this isn't cli
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> printIt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$txt</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$e</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/php_sapi_name"><span style="color: #990000;">php_sapi_name</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;cli&quot;</span>? <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&lt;br&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">print</span> <span style="color: #000088;">$txt</span><span style="color: #339933;">.</span><span style="color: #000088;">$e</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Generates a list of insertable values, escaped for sql
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> make_vals<span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$s</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$k</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$v</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;">$s</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$s</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;, &quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$s</span> <span style="color: #339933;">.=</span> fixMySquirrelVal<span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$s</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Adds a data row to the dest table
 * @param resource $dbin the db connection
 * @param string $table dest table
 * @param array $row indexed array of (string)col =&gt; (mixed)value - do not escape these
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> addRow<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbin</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$vals</span> <span style="color: #339933;">=</span> make_vals<span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$cols</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'`'</span><span style="color: #339933;">.</span><a href="http://www.php.net/join"><span style="color: #990000;">join</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'`,`'</span><span style="color: #339933;">,</span><a href="http://www.php.net/array_keys"><span style="color: #990000;">array_keys</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'`'</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO `<span style="color: #006699; font-weight: bold;">$table</span>` (<span style="color: #006699; font-weight: bold;">$cols</span>) VALUES(<span style="color: #006699; font-weight: bold;">$vals</span>)&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> runMySquirrelQuery<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbin</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Runs a custom query rather than a simple insert
 * @param resource $dbin the db connection
 * @param string $query the custom query, with {field_name} anywhere the field vals will be substituted
 * @param array $row indexed array of (string)col =&gt; (mixed)value - do not escape these
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> customQuery<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbin</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// substitute our new vals</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$k</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/str_replace"><span style="color: #990000;">str_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;{<span style="color: #006699; font-weight: bold;">{$k}</span>}&quot;</span><span style="color: #339933;">,</span> fixMySquirrelVal<span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</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;">// run query with custom vals</span>
  <span style="color: #b1b100;">return</span> runMySquirrelQuery<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbin</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</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;">function</span> fixMySquirrelVal<span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</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><a href="http://www.php.net/is_null"><span style="color: #990000;">is_null</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;NULL&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <a href="http://www.php.net/sprintf"><span style="color: #990000;">sprintf</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; '<span style="color: #009933; font-weight: bold;">%s</span>' &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</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;">function</span> runMySquirrelQuery<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbin</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</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;">$fake_inserts</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$stop_on_failed_query</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$fake_inserts</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    printIt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbin</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;!!ERROR!! Unable to insert record (<span style="color: #006699; font-weight: bold;">$query</span>): &quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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;">$stop_on_failed_query</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Given a row from the source table, converts field names and values
 * to be compatible for dest table
 * @param array $row indexed array of (string)col =&gt; (mixed)value - do not escape these
 * @param array $fields indexed array of (string)source_field =&gt; (mixed)dest_field
 * @param array $defaults indexed array of (string)dest_field =&gt; (mixed)value -- can be null
 * @return array indexed by column names for dest table
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> convertRow<span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fields</span><span style="color: #339933;">,</span> <span style="color: #000088;">$defaults</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// start by applying default values, these can be overwritten if fields contains same value</span>
  <span style="color: #000088;">$newrow</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/is_array"><span style="color: #990000;">is_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$defaults</span><span style="color: #009900;">&#41;</span>? <span style="color: #000088;">$defaults</span> <span style="color: #339933;">:</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$k</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// for multiple fields with same key, strips off the [n] from end</span>
    <span style="color: #000088;">$k</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;@\[[0-9]+\]$@&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #666666; font-style: italic;">// run user functions as needed to modify values</span>
    <span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/is_array"><span style="color: #990000;">is_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span>? <span style="color: #000088;">$v</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$v</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$val</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/is_array"><span style="color: #990000;">is_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span>? <a href="http://www.php.net/call_user_func"><span style="color: #990000;">call_user_func</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #339933;">,</span> <span style="color: #000088;">$k</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/is_null"><span style="color: #990000;">is_null</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
      <span style="color: #666666; font-style: italic;">// create the new column in data row</span>
      <span style="color: #000088;">$newrow</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$val</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;">$newrow</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> connectToDb<span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection_info</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <a href="http://www.php.net/list"><span style="color: #990000;">list</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pass</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbname</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$connection_info</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$dbh</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_connect"><span style="color: #990000;">mysql_connect</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pass</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: #339933;">!</span><span style="color: #000088;">$dbh</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unable to connect to DB: &quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/mysql_select_db"><span style="color: #990000;">mysql_select_db</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbname</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbh</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unable to select <span style="color: #006699; font-weight: bold;">$dbname</span>: &quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$dbh</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> convertTable<span style="color: #009900;">&#40;</span><span style="color: #000088;">$map</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;">$max_rows</span><span style="color: #339933;">;</span>
&nbsp;
  printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;-------------------------------------------------&quot;</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: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$map</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dest_query'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Running custom query: <span style="color: #006699; font-weight: bold;">{$map['dest_query']}</span>&quot;</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>
    printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Migrating values to <span style="color: #006699; font-weight: bold;">{$map['dest_table']}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;-------------------------------------------------&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// we call this each time because we can't just store a separate connection for the from instance</span>
  <span style="color: #666666; font-style: italic;">// and to instance... chances are they point to the same server, so php/mysql will re-use the connection</span>
  <span style="color: #666666; font-style: italic;">// and just switch the instance being operated on... this will cause our queries to go on the fritz</span>
  <span style="color: #666666; font-style: italic;">// the upside is that it doesn't have to reconnect since it's reused, so there's no big cost to this approach</span>
  <span style="color: #000088;">$dbout</span> <span style="color: #339933;">=</span> connectToDb<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$map</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'connection_info'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'source'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// select the old data</span>
  <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$map</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'source_query'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbout</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: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unable to load source data (<span style="color: #006699; font-weight: bold;">{$map['source_query']}</span>): &quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No rows found, that's weird so I'm exiting&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// here again, we call this right before use, because we're going to switch instances, but</span>
  <span style="color: #666666; font-style: italic;">// it's probably to the same mysql server, so connection will get reused</span>
  <span style="color: #000088;">$dbin</span> <span style="color: #339933;">=</span> connectToDb<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$map</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'connection_info'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dest'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// While a row of data exists, put that row in $row as an associative array</span>
  <span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$j</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_assoc"><span style="color: #990000;">mysql_fetch_assoc</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$vals</span> <span style="color: #339933;">=</span> convertRow<span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #339933;">,</span> <span style="color: #000088;">$map</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fields'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$map</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'defaults'</span><span style="color: #009900;">&#93;</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: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$map</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dest_query'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      customQuery<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbin</span><span style="color: #339933;">,</span> <span style="color: #000088;">$map</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dest_query'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$vals</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$j</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      addRow<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbin</span><span style="color: #339933;">,</span> <span style="color: #000088;">$map</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dest_table'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$vals</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$j</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$i</span><span style="color: #339933;">++;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$max_rows</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$max_rows</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">break</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;">// recover query memory</span>
  <a href="http://www.php.net/mysql_free_result"><span style="color: #990000;">mysql_free_result</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// show the user what we did</span>
  printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Converted &quot;</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">-</span><span style="color: #000088;">$j</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; rows&quot;</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;">$j</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;!!!! <span style="color: #006699; font-weight: bold;">$j</span> errors !!!!!!&quot;</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;">// actually run the conversions</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tablemap</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$t</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  convertTable<span style="color: #009900;">&#40;</span><span style="color: #000088;">$t</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Finished without blowing up the planet&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://zenovations.com/blog/2009/04/quick-and-dirty-database-conversion-tool/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>&#8220;undefined function mysql_connect()&#8221; While Migrating Virtualmin</title>
		<link>http://zenovations.com/blog/2009/03/undefined-function-mysql_connect-while-migrating-virtualmin/</link>
		<comments>http://zenovations.com/blog/2009/03/undefined-function-mysql_connect-while-migrating-virtualmin/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 05:55:32 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Virtualmin]]></category>

		<guid isPermaLink="false">http://zenovations.com/blog/?p=3</guid>
		<description><![CDATA[While migrating virtualmin to our shiney new CentOS 5  box, I ran into this error on all our PHP sites: If I look at php info, it shows &#8211;with-mysql, but there is no mysql section farther down. It looked like PHP&#8217;s Apache module was having trouble finding the mysql.so file. I immediately thought to check [...]]]></description>
			<content:encoded><![CDATA[<p>While migrating virtualmin to our shiney new CentOS 5  box, I ran into this error on all our PHP sites:</p>
<p>If I look at php info, it shows &#8211;with-mysql, but there is no mysql section farther down. It looked like PHP&#8217;s Apache module was having trouble finding the mysql.so file. I immediately thought to check for the php-mysql module (there is no php5-mysql for CentOS 5, I noted) and found it installed:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p3code7'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p37"><td class="code" id="p3code7"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server apf<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># yum install php-mysql</span>
...
Parsing package <span style="color: #c20cb9; font-weight: bold;">install</span> arguments
Package php-mysql - 5.1.6-<span style="color: #000000;">20</span>.el5_2.1.i386 is already installed.
Nothing to <span style="color: #000000; font-weight: bold;">do</span></pre></td></tr></table></div>

<p>After a short dive into the php.ini settings, I noted that all the restored sites had this in their php.ini file:</p>
<pre>extension_dir=/usr/lib/<span style="color: #ff0000;">php</span><span style="color: #ff0000;">5</span>/modules</pre>
</p>
<p>But that dir doesn&#8217;t exist. Connecting the dots, I realized that CentOS 5 has moved all the PHP 5 data to the default. So the dir is now at <code>/usr/lib/<span style="color: #ff0000;">php</span>/modules</code>. And this solved the rest:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p3code8'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p38"><td class="code" id="p3code8"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server lib<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># ln -s /usr/lib/php /usr/lib/php5</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server lib<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># ls -ld /usr/lib/php*</span>
drwxr-xr-x <span style="color: #000000;">4</span> root root <span style="color: #000000;">4096</span> Jul <span style="color: #000000;">16</span>  <span style="color: #000000;">2008</span> php
lrwxrwxrwx <span style="color: #000000;">1</span> root root    <span style="color: #000000;">3</span> Mar <span style="color: #000000;">30</span> <span style="color: #000000;">21</span>:01 php5 -<span style="color: #000000; font-weight: bold;">&amp;</span>gt; php
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server lib<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># /etc/init.d/httpd restart</span>
Stopping httpd:                                            <span style="color: #7a0874; font-weight: bold;">&#91;</span>  OK  <span style="color: #7a0874; font-weight: bold;">&#93;</span>
Starting httpd:                                            <span style="color: #7a0874; font-weight: bold;">&#91;</span>  OK  <span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></td></tr></table></div>

<p>Yay!</p>
]]></content:encoded>
			<wfw:commentRss>http://zenovations.com/blog/2009/03/undefined-function-mysql_connect-while-migrating-virtualmin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dropping multiple tables in mysql (drop tables with wildcard)</title>
		<link>http://zenovations.com/blog/2008/08/dropping-multiple-tables-in-mysql-drop-tables-with-wildcard/</link>
		<comments>http://zenovations.com/blog/2008/08/dropping-multiple-tables-in-mysql-drop-tables-with-wildcard/#comments</comments>
		<pubDate>Wed, 06 Aug 2008 17:02:03 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[Web Server]]></category>

		<guid isPermaLink="false">http://zenovations.com/blog/2008/08/dropping-multiple-tables-in-mysql-drop-tables-with-wildcard/</guid>
		<description><![CDATA[I wanted to drop all mysql tables from a db that had a certain prefix, such as xx_. Well this turned out to be an adventure&#8230; The First Attempt The first solution I found was to use sed/grep sort of logic: ?View Code BASH#mysqlshow -u username -p dbname xx\\_% &#124;sed 's/[&#124;+-]//g'&#124;sed 's/[ ]*$/,/'&#62;droptables.sql (xx\\_% is [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to drop all mysql tables from a db that had a certain prefix, such as xx_. </p>
<p>Well this turned out to be an adventure&#8230;</p>
<p><strong>The First Attempt</strong></p>
<p>The first solution I found was to use sed/grep sort of logic:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p109code12'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10912"><td class="code" id="p109code12"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#mysqlshow -u username -p dbname xx\\_% |sed 's/[|+-]//g'|sed 's/[ ]*$/,/'&gt;droptables.sql</span></pre></td></tr></table></div>

<p>(xx\\_% is how you tell it to show tables starting with xx_)</p>
<p>This produced a semi-useful list. But I had to manually edit out the extra commas and line feeds to get a pure list, then add &#8220;DROP TABLE &#8221; to the beginning of the list. Then feed this into mysql as follows:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p109code13'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10913"><td class="code" id="p109code13"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#mysql -u username -p dbname &lt; droptables.sql</span></pre></td></tr></table></div>

<p>I wasn&#8217;t real happy with that answer, naturally, so I researched more&#8230;</p>
<p><strong>A Better Solution</strong></p>
<p>I came across an alternative in this command:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p109code14'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10914"><td class="code" id="p109code14"><pre class="bash" style="font-family:monospace;">mysqldump <span style="color: #660033;">-u</span> username <span style="color: #660033;">-p</span> <span style="color: #660033;">--add-drop-table</span> <span style="color: #660033;">--no-data</span> dbname <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;^DROP.*\`xx_&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> mysql <span style="color: #660033;">-u</span> username <span style="color: #660033;">-p</span> dbname</pre></td></tr></table></div>

<p>(replace xx_ with the prefix you want to remove, to do all tables, try just &#8220;grep ^DROP&#8221;</p>
<p>You can test it first by cutting off the last &#8220;| mysql&#8230;&#8221; bit and see the output, which is useful for debugging, before you go blowing away your tables.</p>
]]></content:encoded>
			<wfw:commentRss>http://zenovations.com/blog/2008/08/dropping-multiple-tables-in-mysql-drop-tables-with-wildcard/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

