Jump to content

[SOLVED] Copying from one database to another database


brooksh

Recommended Posts

I've searched and cannot find a script that works to transfer from one database to another. This is what I have, but it doesn't work. Can anyone help me?

 

 

$resource1 = @mysql_connect("localhost", "username", "password") or die("Could not connect to the database.");
$db = mysql_select_db("database") or die("Could not connect to database.");

$resource2 = @mysql_connect("localhost", "username2", "password2") or die("Could not connect to the database.");
$db = mysql_select_db("database2") or die("Could not connect to database2.");

$sql = "SELECT * FROM table1";
$query = mysql_query($sql, $resource1);
for ($i=0;$i<($result = mysql_fetch_assoc($query));$i++) {
mysql_query("INSERT INTO table2 VALUES * ($resource2");
}

mysql_close($resource1);
mysql_close($resource2);

Here is the solved script

 

mysql_connect("localhost", "username1", "password1") or die("Could not connect to the database.");
mysql_select_db("database1") or die("Could not connect to database1.");


$result1 = mysql_query('SELECT * FROM table1') or exit(mysql_error()); 
while ($row = mysql_fetch_assoc($result1)) 
{ 
    foreach ($row as $key => $value) 
    { 
        $row[$key] =  $key . " = '" . addslashes($value) . "'"; 
    }    
mysql_connect("localhost", "username2", "password2") or die("Could not connect to the database.");
mysql_select_db("database2") or die("Could not connect to database2.");

    $result2 = mysql_query('INSERT INTO table2 SET ' . implode(',', $row)) or exit(mysql_error()); 
     
} 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.