+2  A: 

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
+1  A: 

SQL statements in a loop is a performance issue number one. I think that you could use INSERT SELECT instead of loop

Naktibalda