MySQL
  Home arrow MySQL arrow Page 4 - Remote Database Table Copier
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Remote Database Table Copier
By: Stephen Junker
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 14
    2001-05-07


    Table of Contents:
  • Remote Database Table Copier
  • The Permissions Problem
  • Connecting the World, Two Servers at a Time
  • Copying the Data
  • Adding Some Options
  • Summary
  • Sample Code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Remote Database Table Copier - Copying the Data
    ( Page 4 of 7 )

    After establishing the connections, the next logical step is to move the data from one place to another. This means "SELECT"-ing the data from the source table, and "INSERT"-ing it into the target table. (Assume for now that the target table already exists and has the correct structure to handle the data--we’ll deal with the concept of dynamically building the correct table structures in the next section.)

    This can be done with the code shown below (assuming that you’ve created the connections with the code from the previous section). Rather than provide extensive commentary outside of the code, I’ve added commenting in the code where appropriate to show the function of each section.

    // Return a recordset pointer to all rows in the source table $srcRst = mysql_query("select * from $srcTable;", $srcCnx); $fieldList = ""; $columns = mysql_num_fields($srcRst); // Build the field list for our INSERT statement by iterating through the column names for ($i = 0; $i < $columns; $i++) { $tempField = mysql_field_name($srcRst, $i); $fieldList .= $tempField . ", "; } // Chop off the trailing comma and space ( // I’m sure there are more elegant ways of doing this.) $fieldList = substr($fieldList, 0, strlen($fieldList) - 2); // Iterate through the rows, creating an insert statement for each row while ($srcRow = mysql_fetch_row($srcRst)) { $insertSQL = "insert into $tgtTable"; $insertSQL = " ($fieldList)"; $insertSQL = " VALUES "; // Build a comma-separated value list to match the field list generated above $insertSQL = "("; // Iterate through the columns again, this time grabbing // the value from each column // and building a value list. If values are null, // we want to explicitly insert the null // value, because dates will be set to all zeroes, // if they’re set to an empty string for ($i = 0; $i < $columns; $i++) { if ($srcRow[$i] == null) $insertSQL .= "null, "; else $insertSQL .= "'" . $srcRow[$i] . "', "; } // Again, chop off the comma and space at the end $insertSQL = substr($insertSQL, 0, strlen($insertSQL) - 2); $insertSQL .= ");"; // Execute the finished SQL insert statement on the target table mysql_query($insertSQL, $tgtCnx); } // Free memory from the source recordset. mysql_free_result($srcRst);


    Building the insert statement on the fly is made significantly easier by MySQL’s flexibility with field types when entering data into tables. In my experience with other databases, specifically Access and Visual FoxPro, specific formatting and quoting syntax must be followed for the inserts to work correctly. An ODBC version of this database copying tool would be wonderful, but it would be difficult to deal with the quirks in SQL syntax for each database engine or ODBC driver.

    One other thing I should mention is that copying data this way is probably not appropriate for large tables of more than, say, 10,000 rows. To copy larger sets of data, you would likely have to adjust the timeout settings on your web server, if you have access to it. The timeout for most servers is between 30 and 90 seconds before scripts fail due to timeout, and that may not be enough time to copy exceptionally large tables. I’ve successfully tested the script with tables of up to 5,000 rows without any problems.



     
     
    >>> More MySQL Articles          >>> More By Stephen Junker
     

       

    MYSQL ARTICLES

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT