I am making a PHP web Application in which i am using MySQL as database server, i want to make backup of some tables from one database to another database(with that tables in it).
i have created two different connection, but the table is not updated.
$dbcon1 = mysql_connect(DB_SERVER,DB_USER,DB_PASSWORD) or die(mysql_error());
$dbase1 = mysql_select_db(TEMP_DB_NAME,$dbcon)or die(mysql_error());
$query1=mysql_query("SELECT * FROM emp");
while($row = mysql_fetch_array($query1, MYSQL_NUM))
{
$dbcon2 = mysql_connect(DB_SERVER,DB_USER,DB_PASSWORD) or die(mysql_error());
$dbase2 = mysql_select_db(TEMP_DB_NAME2,$dbcon)or die(mysql_error());
mysql_query("INSERT INTO backup_emp VALUES(null,'$row[1]',$row[2])");
mysql_close($dbcon2);
}
the code above is taking the data of emp from first database, and updataing it into another backup_emp table of another database.
the code is not working properly, is there any other way of doing this…please help.
If both databases they are on the same server, and the tables have the same schema, then you should use this query instead:
This way you don’t need to worry about multiple database connections.