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.

  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



Now its 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.


} else {


$mysql_db = $_REQUEST['d'];

$mysql_user = $_REQUEST['u'];

$mysql_pass = $_REQUEST['p'];

$table_prefix = $_REQUEST['n'];

Were 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;



Now, we want to process the results of our table list query. Were 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
escort Bursa Bursa escort Antalya eskort


- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates


Dev Shed Tutorial Topics: