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.

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

// 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">
			<td>Enter server address<span style="color:red">*</span>:</td>
			<td><input name="s" type="text" id="d" size="50" value="localhost"/></td>
			<td colspan="2" align="center"><span style="color:red">*</span> If you are unsure, do not change this value.</td>
			<td>Enter database name:</td>
			<td><input name="d" type="text" id="d" size="50"/></td>
			<td>Enter database user:</td>
			<td><input name="u" type="text" id="u" size="50"</td/>
			<td>Enter database password:</td>
			<td><input name="p" type="password" id="p" size="50"/></td>
			<td>Enter New Prefix:</td>
			<td><input name="n" type="text" id="n" size="50" value="(Do not include the trailing underscore)"/></td>
			<td>Â </td>
			<td>Â </td>
			<td colspan="2" align="center"><input type="submit" name="Submit" value="Change Table Prefixes"/>
				<input name="action" type="hidden" id="action" value="data"/></td>
} 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;

// 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

function replace_prefix($s, $prefix) {
	$pos = strpos($s, "_");
	$s = substr($s, $pos + 1);
	$s = sprintf("%s_%s", $prefix, $s);
	return $s;


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.