Using two databases
Hi all. I'm using two databases in drupal, one for drupal itself and another to connect with mailman via MySQL. The issue is, if I use de stander form: db_set_active('mailman'); to change to the mailman DB doesn't work, seems stiked on the drupal database. I'm try to create a function that changes the database, but doesn't work at all. So, I have to make the changes using the classic $dbi = mysql_connect("localhost", "USER", "PASS"); mysql_select_db("mailman", $dbi); at the bennging of each function and reconect again to drupal using the same technique. The issue here I don't like the way I do it and I rather preffer the "standard" of db_set_active, but doesn't work. What I'm doing wrong? By the way, I'm using the lastest estable version of Drupal.
/** * Issues a db_query() against the CiviCRM database. */ function civicrm_db_query($query) { global $db_url; // normally Drupal's DSN. $original_url = $db_url; // we'll save it off temporarily. $db_url = CIVICRM_DSN; // set it to the CiviCRM DSN. db_set_active('civicrm'); // then make CiviCRM active. $args = func_get_args(); // load all the args passed. array_shift($args); // shove off the SQL query. $result = db_query($query, $args); // make the query against CiviCRM. $db_url = $original_url; // toggle back to Drupal's DSN. db_set_active('default'); // and make that active again. return $result; // return CiviCRM database result. } -- Morbus Iff ( all of the above - the only checkbox worth checking ) Technical: http://www.oreillynet.com/pub/au/779 Culture: http://www.disobey.com/ and http://www.gamegrene.com/ icq: 2927491 / aim: akaMorbus / yahoo: morbus_iff / jabber.org: morbus
One shouldn't even have to mess with $db_url . I have a bunch of modules which use db_set_active() successfully in Drupal 4.7 and none touch $db_url. Most of my code looks something like this: $previousdb = db_set_active('mydb'); $result = pager_query($sql, $limit, 0, NULL); // handle results db_set_active($previousdb); Morbus, are you finding that your code doesn't function correctly without tweaking the global $db_url? Seems like something might be broken, if so.
One shouldn't even have to mess with $db_url . I have a bunch of modules which use db_set_active() successfully in Drupal 4.7 and none touch $db_url. Most of my code looks something like this:
$previousdb = db_set_active('mydb'); $result = pager_query($sql, $limit, 0, NULL); // handle results db_set_active($previousdb);
Morbus, are you finding that your code doesn't function correctly without tweaking the global $db_url? Seems like something might be
In the above code, where are you specifying the connection details for the new (second) database? -- Morbus Iff ( death to videodrome long live the new flesh ) Technical: http://www.oreillynet.com/pub/au/779 Culture: http://www.disobey.com/ and http://www.gamegrene.com/ icq: 2927491 / aim: akaMorbus / yahoo: morbus_iff / jabber.org: morbus
On 12/12/06, Morbus Iff <morbus@disobey.com> wrote:
In the above code, where are you specifying the connection details for the new (second) database?
In my settings.php file, like this: $db_url['default'] = 'mysql://user:password@server/drupal'; $db_url['alt_db1'] = 'mysql://user:password@host1/database1'; $db_url['alt_db2'] = 'mysql://user:password@host2/database2'; The db_set_active() function seems smart enough to choose from the array, and the core DB layer seems smart enough to make the connection on the first query. IIRC. :-/ Am I missing something? My code works, but I wrote it a long time ago and haven't looked at it since.
participants (3)
-
Chris Johnson -
José Salgado -
Morbus Iff