SunQuest
 
       MySQL
  Home arrow MySQL arrow Page 4 - MySQL Table Prefix Changer Tool in PHP
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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

MySQL Table Prefix Changer Tool in PHP
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 13
    2008-01-02

    Table of Contents:
  • MySQL Table Prefix Changer Tool in PHP
  • Constructing the tool
  • Editing your current database tables
  • Putting the database back together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    MySQL Table Prefix Changer Tool in PHP - Putting the database back together


    (Page 4 of 4 )

    At this point in execution we are left with an array full of table names that have been reconstructed using the new table prefix. Now we need to write that information back to our database.

    // Write new table names back

    foreach ($table_array as $key => $value) {

       $query = sprintf('RENAME TABLE %s TO %s', $table_array[$key], $table_names
    [$key]);

       $result = mysql_query($query, $link);

       if (!$result) {

           $error = mysql_error();

           echo "Could not $query : $error<br>";

       } else {

           $message = sprintf('Successfully renamed %s to %s in %s', $table_array
    [$key], $table_names[$key], $mysql_db);

           echo "$message<br>";

       }

    }

    To do that, we will use a Foreach loop to iterate through each of our array elements. Then it’s just a matter of forming a query that renames the table. Now you can see why we created a duplicate array with our new table names instead of simply editing the existing one.

    We have two side-by-side arrays: one with old names and one with new, in the same order.  By moving through each of them at the same time we can get the old and new table names without ever having to know how many tables we’re even working with.

    // Free the resources

    mysql_close($link);

    }

    All of the dirty work is done. All that’s left is to close our database connection and create the replace_prefix() function that actually determines the new table names for us.

    function replace_prefix($s, $prefix) {

       $pos = strpos($s, "_");

       $s = substr($s, $pos + 1);

       $s = sprintf("%s_%s", $prefix, $s);

       return $s;

    }

    ?>

    Our custom replace_prefix() function is actually pretty simple. It accepts two parameters: the original table name and the new prefix. A few simple string functions parse out the old prefix and replace it with the new.

    Voilà! We’re all done.  Save this with a .php extension and upload it to your web server.  Access your script in your web browser to see it in action. Do not forget to update your site’s configuration settings with the new table prefix information BEFORE running this script; otherwise, you may not be able to access it afterward.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · From the article:"In some cases, they are even able to append them as POST data...
       · Query strings are in fact GET data. Thank you for your correction.
     

       

    MYSQL ARTICLES

    - 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...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...
    - Creating the Blog Script for a PHP/MySQL Blo...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway