Gossamer Forum
Home : Products : DBMan : Customization :

calling one database from another

Quote Reply
calling one database from another
I need dbman to get a record from a second database.
Example: db1 has a field called "lastname". db2 has a field with the same name and value. db1 contains fields "firstname, lastname, job, city", db2 contains fields "firstname, lastname, booktitle, publisher, year".
So when someone searches db1, I want dbman to print out the booktitle for that person together with their job. [just for the example's sake]
I am not using the relational mod, and thought that this particular mod of JPDeni's would do the trick:
http://webmagic.hypermart.net/dbman/multi12.txt

But so far, I couldn't get it to work.
The main code is as follows:

## my code: we have search results in @_ and now compute them for each entry
for (0 .. $numhits - 1) {
my (%rec) = &array_to_hash($_, @hits);
## here starts jpdeni's code
$old_filename = $db_file_name; # assign the current database a different variable
@old_cols = @db_cols; # assign the current database's field names a different array
$db_file_name = $db_script_path . "/db2.db"; # name of second database
@db_cols = ('firstname', 'lastname', 'booktitle', 'publisher', 'year'); # fields of second database
# below is the line that tells the script who you're looking for.
$in{'lastname'} = $rec{'lastname'}; # dbman should search for entries in db2 with the same
# last name than the one from db1 which we have stored in %rec
my ($status2,@hits2) = &query('view'); # I suppose this calls the search routine in db.cgi
if ($status2 eq "ok") { # if we have a match for $rec{'lastname'} in db2 -
%rec2=&array_to_hash(0, @hits); # read the matched record into a hash.
}
$db_file_name = $old_filename; # reassign variable for current database
@db_cols = @old_cols; # reassign the current database's field names
} # end of for loop; skipped printout of actual search returns.

The part up to telling dbman which expression to search on in db2 works fine. However, the loop gets stuck with the first found match: If I have "Miller", "Muller" and "Maller" as last names and Miller has no entry in db2, but Muller and Maller have, the loop gets stuck with Muller and prints out Muller's booktitle etc. for Maller and all following entries.
I guess the problem lies somewhere in the logic of the for loop, but haven't been able to figure out yet where.

kellner