MySQL
  Home arrow MySQL arrow Page 5 - 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 - Adding Some Options
    ( Page 5 of 7 )

    Copying data from one place to another is great, but our script would be much more useful if it could not only copy the data for us, but build the tables that will store the data before copying. By adding that feature, as well as a few others, we have a tool that can be used in a number of different scenarios.

    Building the table on the fly is similar to building a row of data, except that you’ve got to know the data type and length of each field that you’re going to build. PHP has some excellent functionality for obtaining this information not only from a table definition, but from a recordset.pointer as well. The functions are used to dynamically build an SQL create statement to build the table which is executed against the database, as shown below:

    // Select a recordset with all rows from the source table $srcRst = mysql_query("select * from $srcTable;", $srcCnx); $columns = mysql_num_fields($srcRst); // Iterate through each column, getting the name and type of each for ($i = 0; $i < $columns; $i++) { $tempField = mysql_field_name($srcRst, $i); $tempType = mysql_field_type($srcRst, $i); if ($tempType == "string") { // If the type is string, get the size of the field to size correctly. $createSQL .= "$tempField VARCHAR (" . mysql_field_len($srcRst, $i) . "), "; } else $createSQL .= "$tempField $tempType, "; } // Chop the trailing comma and space $createSQL = substr($createSQL, 0, strlen($createSQL) - 2); // Attach the field list to the other parts of the create statement $createSQL = "CREATE TABLE $srcTable ( $createSQL );"; // Run the query, create the table mysql_query($createSQL);

    Unfortunately, this script has limitations. PHP’s mysql_field_type() function does not return exact field type information about each field. Instead, it will return general type information like "string", "int", "real", and "blob". Thus, any varchar, char, or text fields are returned as the string type. While giving enough information to be functional, it’s not entirely accurate. In this script, all "string" fields are interpreted as VARCHAR type, allowing flexibility without taking up a tremendous amount of space.

    Other fields are handled without respect to column size, which could cause a problem for date and blob types. Admittedly, this is one aspect of the project that could be improved, but this is a start, and was acceptable for my purpose at the time.

    The finished script also contains some additional functionality, which I will not explain in great detail here. It will suffice to explain the purpose of each option, and let the sample code speak for itself.
    - Empty a target table before copying data, which was very useful during script testing

    - The option to not copy data, which would allows a user to just create tables based on existing structures or empty tables using the option above without copying any data

    - ‘Verbose’ mode, which simply echoes each SQL statement to the browser before being executed. This should be used carefully on large tables, which would generate thousands or hundreds of thousands on insert statements.

    All of these options can be seen in the completed script presentation at the end of the article. They are controlled by checkboxes on the control form, which turn into boolean switches in the part of the script that actually does the copying.



     
     
    >>> 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 2 Hosted by Hostway
    Stay green...Green IT