[support] Multiple DB connections within hook_cron

Tane Piper digitalspaghetti at googlemail.com
Fri Jan 25 12:48:32 UTC 2008


(apologies if you see this as a duplicate, I had signed up with the
incorrect email and wasn't sure if it'd get through).

Hi Folks,

I'm developing a module at the moment that runs on a cron job.  The
task of the module at the moment is to get all the  users from the
drupal database, then connect to a EMS database and insert the users
into this database.  It allows the site to keep registered users and
mailing list users in sync.

At the moment, I have the two connection strings in the settings.php file:

$db_url['ems'] = 'mysql://client_ems:client_ems@localhost/client_ems';
$db_url['default'] = 'mysql://client:client@localhost/client';

$db_prefix = array(
   'default' => 'main_',
   'ems' => 'ems_',
);


Then within the module, I have my hook_cron function, at the moment
it's just for debugging:

function copycontacts_cron() {

   // First lets get our users
   $get_users_query = db_query("SELECT * FROM {users} WHERE STATUS = 1");

   while ( $user[] = db_fetch_array($get_users_query) ) {
       echo '<xmp>'; print_r($user); echo '</xmp>';
   }

   if (db_set_active('ems')) {
       echo 'DB Selected';
   }
   $moo = db_query('SELECT * FROM {cron}');
   while ($foo = db_fetch_array($moo)) {
       echo $foo;
   }
   db_set_active('default');
}


The function gets as far as the DB Selected message, and then gives me
this error:

Warning: Table 'client_ems.main_system' doesn't exist query: SELECT *
FROM main_system WHERE type = 'theme' in
/path/to/client/public_html/includes/database.mysql.inc on line 172

However I am not making this request, it seems to be coming from
drupal somehow.  Can anyone suggest what might be up?.

-- 
Tane Piper
Blog - http://digitalspaghetti.me.uk
Wii: 4734 3486 7149 1830

This email is: [ ] blogable [ x ] ask first [ ] private


More information about the support mailing list