MySQL - Easiest way to move multiple databases into one / change prefix

Discussion in 'PHP' started by 30k Challenge, Feb 19, 2007.

  1. #1
    Ok I want to take something like database a, b, & c and move them all to a new database. Because the prefixes are the same in a, b, & c those need to be changed as well.

    Is there a SQL command I can run in phpmyadmin to do this or can someone do a script for me to handle it? If so, how much would that cost?

    The databases are on the same server.

    Cheers
     
    30k Challenge, Feb 19, 2007 IP
  2. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #2
    Not sure if this helps but heres my script I run for my directory network. Basically it copies the DB and then goes to 30 different DB and deletes them out and imports the info from the one it copies.

    
    <?php
    $pwd = "password"; 
    $servers = array("DB Name", "DB Name");
    
    
    error_reporting(E_ALL);
    $usr = "db login username";  
    $host = "localhost:3306";  
    $dbname= "Database name of main one to copy";
    $cid = mysql_connect($host,$usr,$pwd); 
    if (!$cid) { die("ERROR: database down: " . mysql_error() . "\n");    }
    $tables = array("tables you wish to copy over", "can include as many as you want");
    $mysqlStr = array();
    
    foreach($tables as $table) {
    	array_push($mysqlStr,  "TRUNCATE TABLE ".$table.";");
    	echo "
    	<br><br><b>TRUNCATE TABLE ".$table.";</b><br>";
    	$result = mysql_db_query($dbname, "SELECT * FROM ".$table."");
    	while ($row = mysql_fetch_row($result)) { 
    		$insertStr = "
    INSERT INTO ".$table." VALUES (";
    		foreach($row as $value) {
    			$insertStr .= "'".str_replace("'", "\'", $value)."', ";
    		}
    		$insertStr = trim($insertStr, ", ").");";
    		array_push($mysqlStr, $insertStr);
    		echo $insertStr."<br>";
    	}
    }
    mysql_close($cid);
    foreach($servers as $server) {
    	$usr = $server; 
    	$dbname= $server."_dir";
    	$cid = mysql_connect($host,$usr,$pwd); 
    	if (!$cid) { die("ERROR: database down: " . mysql_error() . "\n");    }
    	foreach($mysqlStr as $sql) {
    		$result = mysql_db_query($dbname, $sql);
    	}
    	mysql_close($cid);
    }
    	
    ?>
    
    PHP:
    This will only work if all your passwords are the same. I didnt write it but I understand it some if you got any questions about it. But I think it should work for what you are trying to do.

    Just need to change

    $pwd
    $servers
    $tables
    $usr
    $dbname

    to what yours are.
     
    aaron_nimocks, Feb 19, 2007 IP