Open up two connections with mysql_connect(), select the correct database on both and then use the optional link identifier parameter with mysql_query() to choose which database to execute your queries on.
mysql_query ( string $query [, resource $link_identifier ] )
Edit: Also, you might want to have a look at Mysqli (ee.php.net/manual/en/book.mysqli.php) so you can use prepared statements which would probably speed up the looped insert dramatically.
Joonas Trussmann
2010-07-01 13:03:15