How to switch table prefix in a module in Drupal 6?
The prefix is stored as one of the variables in the settings.php file, all of which are made global early on in the page build process.

That in mind, you can do something as easy as:

$GLOBALS['db_prefix'] = 'db1_';
print variable_get('site_name', 'default'); // Site name from first set of tables

$GLOBALS['db_prefix'] = 'db2_';
print variable_get('site_name', 'default'); // Site name from second set of tables


Bear in mind that this change won't persist across page loads so you'll need to do your processing in a single load, or implement some sort of system to track which prefix you should currently be using.