There are lots of snippets for changing a table prefix, but I needed to add a prefix. Here's modified code from another source that gives you the mysql commands to paste into phpmyadmin:

http://www.therefore.ca/transfer/add-prefix.txt (change to .php)

If it's not there at some point, then email me and I'll send it to you. Hopefully it'll save someone some time.

Alex

Comments

Schmidt Attila’s picture

<html>
<head>
<title>MySQL Table Prefix Adder</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</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="75%" border="0" cellspacing="2" cellpadding="2">
		<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>
		</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>&nbsp;</td>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td colspan="2" align="center"><input name="action" type="hidden" id="action" value="data">
				<input type="submit" name="Submit" value="Change Table Prefixes"></td>
		</tr>
	</table>
</form>
<?php
} else {

	$mysql_db = $_REQUEST['d'];
	$mysql_user = $_REQUEST['u'];
	$mysql_pass = $_REQUEST['p'];
	$table_prefix = $_REQUEST['n'];
	
	// Open MySQL link
	$link = mysql_connect('localhost', $mysql_user, $mysql_pass);
	if (!$link) {
			die('Could not connect: ' . mysql_error());
	}
	echo 'Connected successfully<br><br>';
		
		$sql = "SHOW TABLES FROM $mysql_db";
		$result = mysql_query($sql);		
		
		mysql_select_db( $mysql_db, $link);
		
		while ($r = mysql_fetch_array($result)) {
			$table_old = current($r);
			$table_new = $table_prefix.$table_old;
			echo $sql = "RENAME TABLE $table_old TO $table_new;";
			
			$result_rename=mysql_query($sql);
			if (!$result_rename) 
			{
				die('Invalid query: ' . mysql_error());
			}
			
			echo "<br>";
		}
	
	//Free the resources
	mysql_close($link);
}

This is a php script, copy and save as prefix.php name.

szantog’s picture

Just edit your settings.php, change the prefix, then run update.php.

fastballweb’s picture

That's hilarious.

elgandoz’s picture

That gives me an error...

michaelpporter’s picture

The form says not to add the trailing underscore, but I do not see it in the code.

should

$table_new = $table_prefix.$table_old;

be

$table_new = $table_prefix.'_'.$table_old;