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

Editing your current database tables - 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

Now it’s time to get into the meat and potatoes of this thing. We need to break into our else block and construct the code that should be executed in the event that POST data is found.

<?php

} else {

 

$mysql_db = $_REQUEST['d'];

$mysql_user = $_REQUEST['u'];

$mysql_pass = $_REQUEST['p'];

$table_prefix = $_REQUEST['n'];

We’re going to begin by grabbing the POST data set back by our form and pushing that into a few variables for later use.

// Open MySQL link

$link = mysql_connect('localhost', $mysql_user, $mysql_pass);

if (!$link) {

    die('Could not connect: ' . mysql_error());

}

echo 'Connected successfully<br><br>';

Next, we make a MySQL connection using the information provided by the user form submission.

// Select database and grab table list

mysql_select_db($mysql_db, $link) or die ("Database not found.");

$tables = mysql_list_tables($mysql_db);

Then, we connect to the specified database and get a list of its table names.  The mysql_list_tables() function provides a quick mechanism for this.

// Pull table names into an array and replace prefixes

$i = 0;

while ($i < mysql_num_rows($tables)) {

   $table_name = mysql_tablename($tables, $i);

   $table_array[$i] = $table_name;

   $i++;

}

Now, we want to process the results of our table list query. We’re going to parse each table name and put that information into a string array. This will make processing them much more efficient. A While loop serves our needs perfectly.

// Pull table names into another array after replacing prefixes

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

   $table_names[$key] = replace_prefix($value, $table_prefix);

}

Finally, we are going to take each element from this array and build a new array after changing the prefix. We will need to create the replace_prefix() function that makes this all possible.



 
 
>>> 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 9 - Follow our Sitemap

Dev Shed Tutorial Topics: