brooksh Posted July 1, 2007 Share Posted July 1, 2007 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 https://forums.phpfreaks.com/topic/57920-solved-copying-from-one-database-to-another-database/ Share on other sites More sharing options...
brooksh Posted July 1, 2007 Author Share Posted July 1, 2007 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 https://forums.phpfreaks.com/topic/57920-solved-copying-from-one-database-to-another-database/#findComment-287060 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.