Change phpBB Table Prefix

Published by arifur on

Just follow these steps to change the phpBB table prefix.

Step One

Copy the following PHP code.

<html> <head> <title>MySQL Table Prefix Changer</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></meta> </head>

<body> <?php // Check for POST data $action = isset($_REQUEST[‘action’])?$_REQUEST[‘action’]:false;

if (!$action) { ?> <form name="form1" method="post" action="prefix.php"> <table width="50%" border="0" cellspacing="2" cellpadding="2"> <tr> <td>Enter server address<span style="color:red">*</span>:</td> <td><input name="s" type="text" id="d" size="50" value="localhost"/></td> </tr> <tr> <td colspan="2" align="center"><span style="color:red">*</span> If you are unsure, do not change this value.</td> </tr> <tr> <td>Enter database name:</td> <td><input name="d" type="text" id="d" size="50"/></td> </tr> <tr> <td>Enter database user:</td> <td><input name="u" type="text" id="u" size="50"</td/> </td></tr> <tr> <td>Enter database password:</td> <td><input name="p" type="password" id="p" size="50"/></td> </tr> <tr> <td>Enter New Prefix:</td> <td><input name="n" type="text" id="n" size="50" value="(Do not include the trailing underscore)"/></td> </tr> <tr> <td>Â </td> <td>Â </td> </tr> <tr> <td colspan="2" align="center"><input type="submit" name="Submit" value="Change Table Prefixes"/> <input name="action" type="hidden" id="action" value="data"/></td> </tr> </table> </form> <?php } else {

$mysql_server = $_REQUEST[‘s’]; $mysql_db = $_REQUEST[‘d’]; $mysql_user = $_REQUEST[‘u’]; $mysql_pass = $_REQUEST[‘p’]; $table_prefix = $_REQUEST[‘n’];

// Open MySQL link

$link = mysql_connect($mysql_server, $mysql_user, $mysql_pass); if (!$link) { die(‘Could not connect: ‘ . mysql_error()); } echo ‘Connected successfully<br><br />’;

// Select database and grab table list mysql_select_db($mysql_db, $link) or die ("Database not found."); $tables = mysql_list_tables($mysql_db);

// 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++; }

// Pull table names into another array after replacing prefixes foreach ($table_array as $key => $value) { $table_names[$key] = replace_prefix($value, $table_prefix); }

// 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 />"; } }

// Free the resources mysql_close($link); }

function replace_prefix($s, $prefix) { $pos = strpos($s, "_"); $s = substr($s, $pos + 1); $s = sprintf("%s_%s", $prefix, $s); return $s; } ?> </body> </html>

Step Two

Save the code in a PHP file and upload the file to the root directory of the phpBB installation. For example I named the file "php-table-change.php"

Step Three

Now navigate to the absolute location of the uploaded file using any browser. eg: http://localhost/php-table-change.php. Now follow the self explanatory steps and the existing phpBB database table prefix will be changed.



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.