Home arrow MySQL arrow Page 4 - MySQL Table Prefix Changer Tool in PHP

Putting the database back together - MySQL

If you are a web developer, you are undoubtedly aware that there are constant threats to your site. SQL injections are one type of threat that you must be aware of and make every attempt to prevent.

TABLE OF CONTENTS:
  1. MySQL Table Prefix Changer Tool in PHP
  2. Constructing the tool
  3. Editing your current database tables
  4. Putting the database back together
By: Nilpo
Rating: starstarstarstarstar / 24
January 02, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More MySQL Articles          >>> More By Nilpo
 

blog comments powered by Disqus
   

MYSQL ARTICLES

- Xeround Releases Free Version of MySQL Cloud...
- Oracle Announces New MySQL Specialization
- Constant Contact Chooses SkySQL for MySQL Su...
- Revoke Statement in MySQL
- The Grant Statement in MySQL
- SuccessBricks Announces ClearDB Availability...
- Building a PHP ORM: Deploying a Blog
- TROSYS Launches Free MySQL Manager and Admin...
- Building an ORM in PHP: Domain Modeling
- Building an ORM in PHP
- MySQL Leads Open Source Market, Gets Cluster...
- Oracle Announces Milestone Release for MySQL
- How to Stop SQL Injection Attacks
- New Defragmentation Solution for SQL Server
- Comparison of MyISAM and InnoDB MySQL Databa...


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap

Dev Shed Tutorial Topics: