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);

Link to comment
Share on other sites

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()); 
     
} 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.