Multiple DB connections within hook_cron
(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
You don't need to use $db_prefix to use multiple databases. It has an entirely different purpose. It's job is prefix the table names within a given database to prevent collision with other tables of the same name, perhaps being used by another application, or by multiple instances of Drupal sharing the same database. It's mostly there to help folks who have low-end hosting arrangements and have one or limited databases. On Jan 25, 2008 1:48 PM, Tane Piper <digitalspaghetti@googlemail.com> wrote:
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_', );
Quoting Chris Johnson <cxjohnson@gmail.com>:
You don't need to use $db_prefix to use multiple databases. It has an
This isn't the cause of the problem.
entirely different purpose. It's job is prefix the table names within a given database to prevent collision with other tables of the same name, perhaps being used by another application, or by multiple instances of Drupal sharing the same database. It's mostly there to help folks who have low-end hosting arrangements and have one or limited databases.
It's mostly there so that you can prefix table names. If the table names are prefixed then $db_prefix needs to be set. Earnie -- http://for-my-kids.com/ -- http://give-me-an-offer.com/
On Jan 25, 2008 8:17 PM, Earnie Boyd <earnie@users.sourceforge.net> wrote:
Quoting Chris Johnson <cxjohnson@gmail.com>:
You don't need to use $db_prefix to use multiple databases. It has an
This isn't the cause of the problem.
True. But it was obscuring finding the real problem.
Quoting Tane Piper <digitalspaghetti@googlemail.com>:
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?.
Well, I've discovered a couple of these types and have had to add a db_set_active('default') when they crop up. I haven't made the time to issue patches though. This one comes from the modules/system/system.module and the system_region_list() API. You need to add that function <?php $original = db_set_active('default'); ?> to the top of the function and <?php if ($original) { db_set_active($original); } ?> before returning. You may run into one or two more. Earnie -- http://for-my-kids.com/ -- http://give-me-an-offer.com/
participants (3)
-
Chris Johnson -
Earnie Boyd -
Tane Piper