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); Quote Link to comment 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()); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.